package com.boot.web.controller.app.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.aliyuncs.exceptions.ClientException;
import com.boot.constant.Constants;
import com.boot.frame.jwt.JwtToken;
import com.boot.communal.paging.InitPaging;
import com.boot.frame.qiniu.QiNiuConst;
import com.boot.communal.response.ResponseServer;
import com.boot.utils.*;
import com.boot.web.dao.*;
import com.boot.web.dto.*;
import com.boot.web.entity.*;
import com.boot.web.service.CustomerService;
import com.boot.web.service.OrdersCopy30Service;
import com.boot.web.service.OrdersCopyService;
import com.boot.web.service.OrdersService;
import com.boot.web.vo.OrdersVO;
import com.boot.web.vo.SkuAVO;
import com.boot.web.vo.SkuSVO;
import com.boot.web.vo.StoreShopFreightVO;
import com.boot.web.vo.app.*;
import com.github.binarywang.java.emoji.EmojiConverter;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.jdom.JDOMException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 蜜蜂Won APP端关联API
 * @author zhouJie
 */
@RestController
@RequestMapping("/app/api")
public class AppApiController {

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private OrdersDao ordersDao;

    @Autowired
    private SkuRelationDao skuRelationDao;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private AccountRecordDao accountRecordDao;

    @Autowired
    private ClassifyDao classifyDao;

    @Autowired
    private UserCouponDao userCouponDao;

    @Autowired
    private ModeProductDao modeProductDao;

    @Autowired
    private CollageUserDao collageUserDao;

    @Autowired
    private CollageModeDao collageModeDao;

    @Autowired
    private OrderDetailsDao orderDetailsDao;

    @Autowired
    private SpecAttributeDao specAttributeDao;

    @Autowired
    private StoreShopDao storeShopDao;

    @Autowired
    private FailRedPackDao failRedPackDao;

    @Autowired
    private TodayPrizeDao todayPrizeDao;

    @Autowired
    private CollageInRecord30Dao collageInRecord30Dao;

    @Autowired
    private OrdersCopy30Dao ordersCopy30Dao;

    @Autowired
    private CollageInRecord30CopyDao collageInRecord30CopyDao;

    @Autowired
    private AddressDao addressDao;

    @Autowired
    private OrdersCopy30Service ordersCopy30Service;

    @Autowired
    private BannersDao bannersDao;

    @Autowired
    private AppHomePageDao appHomePageDao;

    @Autowired
    private SkuSelectedDao skuSelectedDao;

    @Autowired
    private StoreShopFreightDao storeShopFreightDao;

    @Autowired
    private OpenRedPackDao openRedPackDao;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrdersCopyService ordersCopyService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private PrizePoolDao prizePoolDao;

    @Autowired
    private VersionDao versionDao;

    @Autowired
    private SignInCopyDao signInCopyDao;

    @Autowired
    private SignInDao signInDao;

    @Autowired
    private UserCardDao userCardDao;

    @Autowired
    private CardWithdrawalDao cardWithdrawalDao;

    @Autowired
    private PrizePoolWeeklyDao prizePoolWeeklyDao;

    @Autowired
    private TodayCollageVoucherDao todayCollageVoucherDao;

    @Autowired
    private CouponDao couponDao;

    @Autowired
    private OrdersShareRepeatDao ordersShareRepeatDao;

    @Autowired
    private LevelDao levelDao;

    @Autowired
    private CacheCodeDao cacheCodeDao;

    @Autowired
    private WithdrawalDao withdrawalDao;

    @Autowired
    private WechatAlipayDao wechatAlipayDao;

    @Autowired
    private RedPackSpecDao redPackSpecDao;

    @Autowired
    private LevelCopy4Dao levelCopy4Dao;

    @Autowired
    private TodayCollageProfitDao todayCollageProfitDao;

    @Autowired
    private PrizePoolLoveDao prizePoolLoveDao;

    @Autowired
    private PrizePoolAreanDao prizePoolAreanDao;

    @Autowired
    private LevelCopy2Dao levelCopy2Dao;

    @Autowired
    private LevelCopy3Dao levelCopy3Dao;

    @Autowired
    private CollageTotalVoucherDao collageTotalVoucherDao;


    /**
     * 获取App版本数据
     * @param type
     * @return
     */
    @GetMapping("/getVersionData")
    public ResponseServer getVersionData(String type) {
        Version version = versionDao.queryVersionByType(type);
        return ResponseServer.success("成功", version);
    }


    /**
     * 获取七牛云token
     * @return
     */
    @GetMapping("/getQiNiuToken")
    public ResponseServer getQiNiuToken() {
        QiNiuDTO dto = new QiNiuDTO();
        dto.setPrefix("http://img.jiajiani.cn");
        String qiNiuToken = QiNiuConst.getToken();
        dto.setToken(qiNiuToken);
        return ResponseServer.success("成功", dto);
    }


    /**
     * 同步客户数据
     * @param customer
     * @return
     */
    public AppCustomerVO synchronous(Customer customer){
        AppCustomerVO vo = new AppCustomerVO();
        BeanUtils.copyProperties(customer, vo);
        vo.setLevelName(levelDao.queryById(customer.getLevelId()).getName()); // 拼团等级名称
        vo.setLevelCopy4Name(levelCopy4Dao.queryById(customer.getLevelCopy4Id()).getName()); // 共享V等级名称
        vo.setParentCount(customerDao.queryCustomerByParentId(customer.getId()).size());
        vo.setWaitingCount(ordersDao.queryOrdersVOByStatus(customer.getId(), "5").size()); //等开奖
        vo.setDeliveredCount(ordersDao.queryOrdersVOByStatus(customer.getId(), "1").size()); //待发货
        vo.setReceivedCount(ordersDao.queryOrdersVOByStatus(customer.getId(), "2").size()); //待收货
        vo.setAfterSalesCount(ordersDao.queryOrdersVOByStatus(customer.getId(), "7").size()); //代售
        vo.setCollageCount(ordersDao.queryOrdersVOByStatus(customer.getId(), "6").size()); //待开团
        Account account = accountDao.queryAccountByUserId(customer.getId());
        vo.setBliss(account.getBliss());
        vo.setIntegral(account.getIntegral());
        vo.setVoucherBalance(account.getVoucherBalance());
        vo.setRedPacket(account.getRedPacket());
        vo.setCollageBalance(account.getCollageBalance());
        vo.setShareBalance(account.getShareBalance());
        vo.setFrozenIn(account.getFrozenIn());
        if(customer.getParentId() == 1198){
            vo.setParentAgentCode("");
        }else{
            vo.setParentAgentCode(customerDao.queryById(customer.getParentId()).getAgentCode());
        }
        return vo;
    }


    /**
     * 微信登录
     * @param parameter
     * @return
     */
    @PostMapping("/login_wx")
    public ResponseServer login_wx(@RequestBody String parameter){
        System.err.println("微信登录********** parameter ---------- : " + parameter);
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String unionId = jsonObject.get("unionId").toString();
        String openId = jsonObject.get("openId").toString();
        String nickName = jsonObject.get("nickName").toString();
        String headImg = jsonObject.get("headImg").toString();
        if(unionId == null && "".equals(unionId)){
            return ResponseServer.error("授权失败,unionId为空!");
        }
        Customer customer = customerDao.queryCustomerByUnionId(unionId);
        if(customer != null){
            // 更新下字段值
            customer.setUnionId(unionId);
            customer.setOpenId(openId);
            customer.setNickName(nickName);
            customer.setHeadImg(headImg);
            customerDao.update(customer);
            if(customer.getMobile() == null && "".equals(customer.getMobile())){
                return ResponseServer.unbound_phone1008("未绑定手机号码(有unionId)");  // 1008 有unionId 无手机号
            }
            return ResponseServer.success("微信登录成功", synchronous(customer));
        }
        // 注册
        Customer newCustomer = new Customer();
        newCustomer.setUnionId(unionId);
        newCustomer.setOpenId(openId);
        newCustomer.setNickName(nickName);
        newCustomer.setHeadImg(headImg);
        newCustomer.setParentId(1198);
        BigDecimal decimal = new BigDecimal(0);
        newCustomer.setLevelId(1);
        newCustomer.setLevelCopyId(1);
        newCustomer.setLevelCopy1Id(1);
        newCustomer.setLevelCopy2Id(1);
        newCustomer.setLevelCopy3Id(1);
        newCustomer.setLevelCopy4Id(1);
        newCustomer.setIsActive("0");
        newCustomer.setIsActiveCopy("0");
        newCustomer.setIsActiveCopy1("0");
        newCustomer.setIsActiveCopy2("0");
        newCustomer.setIsActiveCopy3("0");
        newCustomer.setIsActiveCopy4("1");
        newCustomer.setAchievement(decimal);
        newCustomer.setAchievement3(decimal);
        newCustomer.setQuantityRecord(0);
        newCustomer.setOwnTotalBuy(decimal);
        newCustomer.setOwnTotalBuy3(decimal);
        newCustomer.setFiction("0");
        newCustomer.setAddUpAchievement(decimal);
        newCustomer.setAddUpUpgrade(decimal);
        newCustomer.setAddUpUpgradeFiction(decimal);
        newCustomer.setNickName(nickName);
        newCustomer.setUpdateTime(new Date());
        newCustomer.setVerifyStatus("3");
        newCustomer.setEffective(0);
        newCustomer.setOrderQuantity(0);
        newCustomer.setIsMember("1"); // 全部默认为会员
        newCustomer.setIsDel("0");
        newCustomer.setRepeatAmount(new BigDecimal(0));
        newCustomer.setTodayArean(new BigDecimal(0));
        customerDao.save(newCustomer);
        String token = JwtToken.createToken(newCustomer.getId());
        newCustomer.setToken(token);
        customerDao.update(newCustomer);
        if(accountDao.queryAccountByUserId(newCustomer.getId()) == null){
            Account account = new Account();
            account.setIntegral(0);
            account.setBliss(0);
            account.setRedPacket(0);
            account.setVoucherBalance(decimal);
            account.setUpdateTime(new Date());
            account.setUserId(newCustomer.getId());
            account.setCollageBalance(decimal);
            account.setShareBalance(decimal);
            account.setFrozenIn(decimal);
            accountDao.save(account);
            List<Coupon> couponList = couponDao.queryAll();
            if(couponList.size() > 0){
                for(Coupon cou : couponList){
                    UserCoupon uc = userCouponDao.queryByUserIdAndCouponId(newCustomer.getId(), cou.getId());
                    if(uc == null){
                        UserCoupon newUc= new UserCoupon();
                        newUc.setCouponId(cou.getId());
                        newUc.setUserId(newCustomer.getId());
                        newUc.setUpdateTime(new Date());
                        newUc.setQuantity(0);
                        userCouponDao.save(newUc);
                    }
                }
            }
        }
        return ResponseServer.unbound_phone1009("未绑定手机号码(无unionId)"); // 1009 无unionId 再判断手机号有没有
    }


    /**
     * 绑定手机号码
     * @param parameter  [type] 返回code=1008 传递type=1 / 返回code=1009 传递type=2
     * @return
     */
    @PostMapping("/bindingPhone")
    public ResponseServer bindingPhone(@RequestBody String parameter){
        System.err.println("绑定手机号码********** parameter ---------- : " + parameter);
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String unionId = jsonObject.get("unionId").toString();
        String phone = jsonObject.get("phone").toString();
        String code = jsonObject.get("code").toString();
        String type = jsonObject.get("type").toString();
        Customer customer1 = customerDao.queryCustomerByUnionId(unionId);
        if(customer1 == null){
            return ResponseServer.error("unionId 不能为空");
        }
        CacheCode cc = cacheCodeDao.queryByInfo(phone, "3");
        //String checkCode = JedisOperate.getObject(phone).toString();
        if (!code.equals(cc.getCode())) {
            return ResponseServer.error("验证码不正确!");
        }
        Customer customer = customerDao.queryCustomerByMobile(phone);
        if(customer != null){
            if(customer.getUnionId() == null && "".equals(customer.getUnionId())){
                // 1008
                if("1".equals(type)){
                    // 将 phone 绑定到 unionId 上
                    customer1.setMobile(customer.getMobile());
                    customer1.setRemarks("[1008]账号ID：" + customer.getId() + "，合并到账号ID：" + customer1.getId());
                    customer1.setLoginName(customer.getLoginName()); // 登录名
                    customer1.setPassword(customer.getPassword()); // 密码
                    customer1.setMasterAccount(customer.getMasterAccount()); // 主账号
                    // 刷新token
                    String token = JwtToken.createToken(customer1.getId());
                    customer1.setToken(token);
                    customerDao.update(customer1);
                    // 将 phone 账号 置空
                    customer.setParentId(1198);
                    customer.setAgentCode("");
                    customer.setMobile("");
                    customer.setToken("");
                    customer.setUnionId("");
                    customer.setOpenId("");
                    customer.setIdCard("");
                    customer.setRemarks("[1008]当前账号被合并到了，账号ID：" + customer1.getId());
                    customer.setLoginName("");
                    customer.setMasterAccount("");
                    customerDao.update(customer);
                    // 将 customer 的下级 全部移到 customer1 下
                    List<Customer> cList = customerDao.queryCustomerByParentId(customer.getId());
                    if(cList.size() > 0){
                        for(Customer c : cList){
                            c.setParentId(customer1.getId());
                            customerDao.update(c);
                        }
                    }
                    return ResponseServer.success("绑定手机号码成功", synchronous(customer1));
                }else{
                    // 1009
                    // 将 unionId 合并到 phone 上
                    customer.setOpenId(customer1.getOpenId());
                    customer.setUnionId(customer1.getUnionId());
                    customer.setNickName(customer1.getNickName());
                    customer.setHeadImg(customer1.getHeadImg());
                    customer.setRemarks("[1009]账号ID：" + customer1.getId() + "，合并到账号ID：" + customer.getId());
                    // 刷新token
                    String token = JwtToken.createToken(customer.getId());
                    customer.setToken(token);
                    customerDao.update(customer);
                    // 将 unionId 账号 置空
                    customer1.setParentId(1198);
                    customer1.setAgentCode("");
                    customer1.setMobile("");
                    customer1.setToken("");
                    customer1.setUnionId("");
                    customer1.setOpenId("");
                    customer1.setIdCard("");
                    customer1.setRemarks("[1009]当前账号被合并到了，账号ID：" + customer.getId());
                    customerDao.update(customer1);
                    return ResponseServer.success("绑定手机号码成功", synchronous(customer));
                }
            }else{
                return ResponseServer.error("手机号码已存在!");
            }
        }
        // 直接绑定手机号码
        String token = JwtToken.createToken(customer1.getId());
        customer1.setToken(token);
        customer1.setMobile(phone);
        customer1.setMasterAccount(phone);
        customer1.setPassword("bee" + phone);
        customerDao.update(customer1);
        return ResponseServer.success("绑定手机号码成功", synchronous(customer1));
    }


    /**
     * 登录 - 账号/密码
     * @param parameter
     * @return
     */
    @PostMapping("/login_p")
    public ResponseServer login_p(@RequestBody String parameter){
        System.err.println("登录 - 账号/密码********** parameter ---------- : " + parameter);
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String loginName = jsonObject.get("loginName").toString();
        String password = jsonObject.get("password").toString();
        Customer customer1 = customerDao.queryCustomerByMobile(loginName);
        if(customer1 == null){
            Customer customer = customerDao.queryCustomerByLoginName(loginName);
            if(customer == null){
                return ResponseServer.error("账号不存在!");
            }
            if(!customer.getPassword().equals(password)){
                return ResponseServer.error("登录密码不正确!");
            }
            String token = JwtToken.createToken(customer.getId());
            customer.setToken(token);
            customerDao.update(customer);
            return ResponseServer.success("登录成功", synchronous(customer));
        }
        if(!customer1.getPassword().equals(password)){
            return ResponseServer.error("登录密码不正确!");
        }
        customer1.setLoginName(customer1.getMobile());
        customer1.setMasterAccount(customer1.getMobile()); // 老用户手机号自动设置为主账户
        String token = JwtToken.createToken(customer1.getId());
        customer1.setToken(token);
        customerDao.update(customer1);
        return ResponseServer.success("登录成功", synchronous(customer1));
    }


    /**
     * 退出登录
     * @param request
     * @return
     */
    @PostMapping("/login_out")
    public ResponseServer login_p(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        customer.setToken("");
        customerDao.update(customer);
        return ResponseServer.success("退出成功");
    }


    /**
     * 发送短信验证码
     * @param parameter [type] (0=登录，1=注册，2=重置密码，3=绑定手机号)
     * @return
     * @throws ClientException
     */
    @PostMapping("/sendCode")
    public ResponseServer sendCode(@RequestBody String parameter) throws ClientException {
        System.err.println("发送短信验证码********** parameter ---------- : " + parameter);
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String phone = jsonObject.get("phone").toString();
        String type = jsonObject.get("type").toString();
        if("1".equals(type) || "3".equals(type)){
            Customer customer = customerDao.queryCustomerByMobile(phone);
            if(customer != null){
                return ResponseServer.error("手机号码已存在!");
            }
        }else{
            Customer customer = customerDao.queryCustomerByMobile(phone);
            if(customer == null){
                return ResponseServer.error("手机号码不存在!");
            }
        }
        //if(JedisOperate.exists(phone)){
            //return ResponseServer.error("请60秒后再发送验证码!");
        //}else{
            RandomNumberUtil rn = new RandomNumberUtil();
            String code = rn.verificationCode();
            CacheCode cc = cacheCodeDao.queryByInfo(phone, type);
            if(cc == null){
                CacheCode newCc = new CacheCode();
                newCc.setPhone(phone);
                newCc.setType(type);
                newCc.setCode(code);
                cacheCodeDao.save(newCc);
                SendSmsUtil.sendSms(phone, code);
                return ResponseServer.success("发送成功");
            }else{
                cc.setCode(code);
                cacheCodeDao.update(cc);
                SendSmsUtil.sendSms(phone, code);
                return ResponseServer.success("发送成功");
            }
            // 发送验证码并保存
            //RandomNumberUtil rn = new RandomNumberUtil();
            //String code = rn.verificationCode();
            //JedisOperate.setObject(phone, code, 60); // 60秒过期
            //SendSmsUtil.sendSms(phone, code);
            //return ResponseServer.success("发送成功");
        //}
    }


    /**
     * 登录 - 手机号/验证码
     * @param parameter
     * @return
     */
    @PostMapping("/login_c")
    public ResponseServer login_c(@RequestBody String parameter){
        System.err.println("登录 - 手机号/验证码********** parameter ---------- : " + parameter);
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String phone = jsonObject.get("phone").toString();
        String code = jsonObject.get("code").toString();
        Customer customer = customerDao.queryCustomerByMobile(phone);
        if(customer == null){
            return ResponseServer.error("手机号码不存在!");
        }
        CacheCode cc = cacheCodeDao.queryByInfo(phone, "0");
        //String checkCode = JedisOperate.getObject(phone).toString();
        if (!code.equals(cc.getCode())) {
            return ResponseServer.error("验证码不正确!");
        }
        customer.setLoginName(customer.getMobile());
        customer.setMasterAccount(customer.getMobile()); // 老用户手机号自动设置为主账户
        String token = JwtToken.createToken(customer.getId());
        customer.setToken(token);
        customerDao.update(customer);
        return ResponseServer.success("登录成功", synchronous(customer));
    }


    /**
     * 注册
     * @param parameter
     * @return
     */
    @PostMapping("/register")
    public ResponseServer register(@RequestBody String parameter){
        System.err.println("注册********** parameter ---------- : " + parameter);
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String phone = jsonObject.get("phone").toString();
        String code = jsonObject.get("code").toString();
        String password = jsonObject.get("password").toString();
        String agentCode = jsonObject.get("agentCode").toString();
        Customer customer = customerDao.queryCustomerByMobile(phone);
        if(customer != null){
            return ResponseServer.error("手机号码已存在!");
        }
        // 需要判断一下key是否还存在 不存在则redis缓存数据过期了 提示验证码失效了
        //if(JedisOperate.exists(JedisOperate.getObject(phone).toString())){

        //}
        CacheCode cc = cacheCodeDao.queryByInfo(phone, "1");
        //String checkCode = JedisOperate.getObject(phone).toString();
        if (!code.equals(cc.getCode())) {
            return ResponseServer.error("验证码不正确!");
        }
        Customer newCustomer = new Customer();
        if(agentCode != null && !"".equals(agentCode)){
            Customer customer1 = customerDao.queryCustomerByAgentCode(agentCode); // 邀请码就是用户的编号
            if(customer1 == null){
                return ResponseServer.error("邀请码不存在!");
            }
            // 生成编号
            if (customer1.getId() != 1198) {
                if(customer1.getAgentCode() != null && !"".equals(customer1.getAgentCode())){
                    String str = customer1.getAgentCode().substring(0,3);
                    String newCode;
                    do {
                        newCode = str + randoms();
                        if (customerDao.queryCustomerByAgentCode(newCode) == null) {
                            break;
                        }
                    } while (customerDao.queryCustomerByAgentCode(newCode) != null);
                    newCustomer.setAgentCode(newCode);
                    newCustomer.setParentId(customer1.getId());
                    //newCustomer.setSign(String.valueOf(customer1.getId()));
                }else{
                    newCustomer.setAgentCode("");
                }
            } else {
                newCustomer.setParentId(1198);
            }
        }else{
            newCustomer.setParentId(1198);
        }
        BigDecimal decimal = new BigDecimal(0);
        newCustomer.setLevelId(1);
        newCustomer.setLevelCopyId(1);
        newCustomer.setLevelCopy1Id(1);
        newCustomer.setLevelCopy2Id(1);
        newCustomer.setLevelCopy3Id(1);
        newCustomer.setLevelCopy4Id(1);
        newCustomer.setIsActive("0");
        newCustomer.setIsActiveCopy("0");
        newCustomer.setIsActiveCopy1("0");
        newCustomer.setIsActiveCopy2("0");
        newCustomer.setIsActiveCopy3("0");
        newCustomer.setIsActiveCopy4("1");
        newCustomer.setAchievement(decimal);
        newCustomer.setAchievement3(decimal);
        newCustomer.setQuantityRecord(0);
        newCustomer.setOwnTotalBuy(decimal);
        newCustomer.setOwnTotalBuy3(decimal);
        newCustomer.setFiction("0");
        newCustomer.setAddUpAchievement(decimal);
        newCustomer.setAddUpUpgrade(decimal);
        newCustomer.setAddUpUpgradeFiction(decimal);
        newCustomer.setNickName(phone);
        newCustomer.setMobile(phone);
        newCustomer.setUpdateTime(new Date());
        newCustomer.setPassword(password); // 密码
        newCustomer.setLoginName(phone); // 登录名
        newCustomer.setMasterAccount(phone); // 主账号
        newCustomer.setVerifyStatus("3");
        newCustomer.setEffective(0);
        newCustomer.setOrderQuantity(0);
        newCustomer.setIsMember("1"); // 全部默认为会员
        newCustomer.setIsDel("0");
        newCustomer.setRepeatAmount(new BigDecimal(0));
        newCustomer.setTodayArean(new BigDecimal(0));
        customerDao.save(newCustomer);
        String token = JwtToken.createToken(newCustomer.getId());
        newCustomer.setToken(token);
        customerDao.update(newCustomer);
        if(accountDao.queryAccountByUserId(newCustomer.getId()) == null){
            Account account = new Account();
            account.setIntegral(0);
            account.setBliss(0);
            account.setRedPacket(0);
            account.setVoucherBalance(decimal);
            account.setUpdateTime(new Date());
            account.setUserId(newCustomer.getId());
            account.setCollageBalance(decimal);
            account.setShareBalance(decimal);
            account.setFrozenIn(decimal);
            accountDao.save(account);
            List<Coupon> couponList = couponDao.queryAll();
            if(couponList.size() > 0){
                for(Coupon cou : couponList){
                    UserCoupon uc = userCouponDao.queryByUserIdAndCouponId(newCustomer.getId(), cou.getId());
                    if(uc == null){
                        UserCoupon newUc= new UserCoupon();
                        newUc.setCouponId(cou.getId());
                        newUc.setUserId(newCustomer.getId());
                        newUc.setUpdateTime(new Date());
                        newUc.setQuantity(0);
                        userCouponDao.save(newUc);
                    }
                }
            }
        }
        AppCustomerVO vo = new AppCustomerVO();
        BeanUtils.copyProperties(newCustomer, vo);
        vo.setParentCount(0);
        vo.setWaitingCount(0); //等开奖
        vo.setDeliveredCount(0); //待发货
        vo.setReceivedCount(0); //待收货
        vo.setAfterSalesCount(0); //代售
        vo.setCollageCount(0); //待开团
        vo.setBliss(0);
        vo.setIntegral(0);
        vo.setVoucherBalance(decimal);
        vo.setRedPacket(0);
        if(newCustomer.getParentId() == 1198){
            vo.setParentAgentCode("");
        }else{
            vo.setParentAgentCode(customerDao.queryById(newCustomer.getParentId()).getAgentCode());
        }
        return ResponseServer.success("注册成功", vo);
    }


    /**
     * 忘记密码 - 重置
     * @param parameter
     * @return
     */
    @PostMapping("/forgetPassword")
    public ResponseServer forgetPassword(@RequestBody String parameter){
        System.err.println("忘记密码 - 重置********** parameter ---------- : " + parameter);
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String phone = jsonObject.get("phone").toString();
        String code = jsonObject.get("code").toString();
        String newPassword = jsonObject.get("newPassword").toString();
        Customer customer = customerDao.queryCustomerByMobile(phone);
        if(customer == null){
            return ResponseServer.error("手机号码不存在!");
        }
        //String checkCode = JedisOperate.getObject(phone).toString();
        CacheCode cc = cacheCodeDao.queryByInfo(phone, "2");
        if (!code.equals(cc.getCode())) {
            return ResponseServer.error("验证码不正确!");
        }
        customer.setPassword(newPassword);
        String token = JwtToken.createToken(customer.getId());
        customer.setToken(token);
        customerDao.update(customer);
        return ResponseServer.success("密码重置成功");
    }


    /**
     * 刷新客户信息
     * @param request
     * @return
     */
    @PostMapping("/refreshCustomer")
    public ResponseServer refreshCustomer(HttpServletRequest request){
        // String appVersion = request.getHeader("x-app-version"); // APP版本号
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        return ResponseServer.success("刷新成功", synchronous(customer));
    }


    /**
     * 绑定上级
     * @param request
     * @param parameter
     * @return
     */
    @PostMapping("/bindingParent")
    public ResponseServer bindingParent(@RequestBody String parameter, HttpServletRequest request){
        System.err.println("绑定上级********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String agentCode = jsonObject.get("agentCode").toString();
        Customer pc = customerDao.queryCustomerByAgentCode(agentCode);
        if(pc == null){
            return ResponseServer.error("编号不存在!");
        }
        String str = pc.getAgentCode().substring(0,3);
        String newCode;
        do {
            newCode = str + randoms();
            if (customerDao.queryCustomerByAgentCode(newCode) == null) {
                break;
            }
        } while (customerDao.queryCustomerByAgentCode(newCode) != null);
        customer.setAgentCode(newCode);
        customer.setParentId(pc.getId());
        customerDao.update(customer);
        return ResponseServer.success("绑定编号成功", synchronous(customer));
    }


    /**
     * 创建子账号
     * @param request
     * @param parameter
     * @return
     */
    @PostMapping("/createSubAccount")
    public ResponseServer createSubAccount(@RequestBody String parameter, HttpServletRequest request){
        System.err.println("创建子账号********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
//        if(customer.getMobile() == null && "".equals(customer.getMobile())){
//            return ResponseServer.error("子账号不可创建子账号!");
//        }
        List<AppAccountNumberVO> voList = customerDao.queryCustomerByMasterAccount5(customer.getMasterAccount());
        if(voList.size() > 5){
            return ResponseServer.error("子账号不超过五个!");
        }
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String headImg = jsonObject.get("headImg").toString();
        String loginName = jsonObject.get("loginName").toString();
        String password = jsonObject.get("password").toString();
        Customer sub = customerDao.queryCustomerByLoginName(loginName);
        if(sub != null){
            return ResponseServer.error("当前账号已存在!");
        }
        // 创建子新账号
        Customer newCustomer = new Customer();
        if(customer.getAgentCode() != null && !"".equals(customer.getAgentCode()) && customer.getId() != 1198){
            // 生成编号
            String str = customer.getAgentCode().substring(0,3);
            String newCode;
            do {
                newCode = str + randoms();
                if (customerDao.queryCustomerByAgentCode(newCode) == null) {
                    break;
                }
            } while (customerDao.queryCustomerByAgentCode(newCode) != null);
            newCustomer.setAgentCode(newCode);
            newCustomer.setParentId(customer.getId());
            //newCustomer.setSign(String.valueOf(customer.getId()));
        }else{
            return ResponseServer.error("当前账号没有编号, 不能创建子账号!");
        }
        BigDecimal decimal = new BigDecimal(0);
        newCustomer.setPassword(password);
        newCustomer.setLevelId(1);
        newCustomer.setLevelCopyId(1);
        newCustomer.setLevelCopy1Id(1);
        newCustomer.setLevelCopy2Id(1);
        newCustomer.setLevelCopy3Id(1);
        newCustomer.setLevelCopy4Id(1);
        newCustomer.setIsActive("0");
        newCustomer.setIsActiveCopy("0");
        newCustomer.setIsActiveCopy1("0");
        newCustomer.setIsActiveCopy2("0");
        newCustomer.setIsActiveCopy3("0");
        newCustomer.setIsActiveCopy4("1");
        newCustomer.setAchievement(decimal);
        newCustomer.setAchievement3(decimal);
        newCustomer.setQuantityRecord(0);
        newCustomer.setOwnTotalBuy(decimal);
        newCustomer.setOwnTotalBuy3(decimal);
        newCustomer.setFiction("0");
        newCustomer.setAddUpAchievement(decimal);
        newCustomer.setAddUpUpgrade(decimal);
        newCustomer.setAddUpUpgradeFiction(decimal);
        newCustomer.setLoginName(loginName);
        newCustomer.setPassword(password);
        newCustomer.setUpdateTime(new Date());
        newCustomer.setMasterAccount(customer.getMasterAccount());
        newCustomer.setHeadImg(headImg);
        newCustomer.setVerifyStatus("3");
        newCustomer.setEffective(0);
        newCustomer.setOrderQuantity(0);
        newCustomer.setIsMember("1"); // 全部默认为会员
        newCustomer.setIsDel("0");
        newCustomer.setRepeatAmount(new BigDecimal(0));
        newCustomer.setTodayArean(new BigDecimal(0));
        customerDao.save(newCustomer);
        if(accountDao.queryAccountByUserId(newCustomer.getId()) == null){
            Account account = new Account();
            account.setIntegral(0);
            account.setBliss(0);
            account.setRedPacket(0);
            account.setVoucherBalance(decimal);
            account.setUpdateTime(new Date());
            account.setUserId(newCustomer.getId());
            account.setCollageBalance(decimal);
            account.setShareBalance(decimal);
            account.setFrozenIn(decimal);
            accountDao.save(account);
            List<Coupon> couponList = couponDao.queryAll();
            if(couponList.size() > 0){
                for(Coupon cou : couponList){
                    UserCoupon uc = userCouponDao.queryByUserIdAndCouponId(newCustomer.getId(), cou.getId());
                    if(uc == null){
                        UserCoupon newUc= new UserCoupon();
                        newUc.setCouponId(cou.getId());
                        newUc.setUserId(newCustomer.getId());
                        newUc.setUpdateTime(new Date());
                        newUc.setQuantity(0);
                        userCouponDao.save(newUc);
                    }
                }
            }
        }
        AppCustomerVO vo = new AppCustomerVO();
        BeanUtils.copyProperties(newCustomer, vo);
        vo.setParentCount(0);
        vo.setWaitingCount(0); //等开奖
        vo.setDeliveredCount(0); //待发货
        vo.setReceivedCount(0); //待收货
        vo.setAfterSalesCount(0); //代售
        vo.setCollageCount(0); //待开团
        vo.setBliss(0);
        vo.setIntegral(0);
        vo.setVoucherBalance(decimal);
        vo.setRedPacket(0);
        String token = JwtToken.createToken(newCustomer.getId());
        newCustomer.setToken(token);
        customerDao.update(newCustomer);
        return ResponseServer.success("创建子账号成功", vo);
    }


    /**
     * 切换账号登录
     * @param request
     * @param parameter
     * @return
     */
    @PostMapping("/switchSubAccountLogin")
    public ResponseServer switchSubAccountLogin(@RequestBody String parameter, HttpServletRequest request){
        System.err.println("切换账号登录********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        Integer userId = (Integer) jsonObject.get("userId");
        Customer sub = customerDao.queryById(userId);
        if(sub == null){
            return ResponseServer.error("切换账号不存在!");
        }
        String token = JwtToken.createToken(sub.getId());
        sub.setToken(token);
        customerDao.update(sub);
        return ResponseServer.success("创建子账号成功", synchronous(sub));
    }


    /**
     * 获取客户账号关联数据
     * @param request
     * @return
     */
    @GetMapping("/getAccountNumberRelationData")
    public ResponseServer getAccountNumberRelationData(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        Map map = new HashMap();
        AppAccountNumberVO vo = new AppAccountNumberVO();
        vo.setUserId(customer.getId());
        vo.setHeadImg(customer.getHeadImg());
        vo.setLoginName(customer.getLoginName());
        vo.setSign("当前登录账户");
        map.put("currentLoginAccount", vo);

        Customer customer1 = customerDao.queryCustomerByMobile(customer.getMasterAccount());
        if(customer1 != null){
            AppAccountNumberVO vo1 = new AppAccountNumberVO();
            vo1.setUserId(customer1.getId());
            vo1.setHeadImg(customer1.getHeadImg());
            vo1.setLoginName(customer1.getNickName());
            vo1.setSign("主账户");
            map.put("masterAccount", vo1);
        }

        if(customer.getMobile() != null && !"".equals(customer.getMobile())){
            // customer是主账号
            List<AppAccountNumberVO> customerList = customerDao.queryCustomerByMasterAccount(customer.getMasterAccount(), customer1.getId());
            if(customerList.size() > 0){
                for(AppAccountNumberVO customer2 : customerList){
                    customer2.setSign("子账户");
                }
                map.put("subAccount", customerList);
            }
        }else{
            // customer是子账号
            List<AppAccountNumberVO> customerList = customerDao.queryCustomerByMasterAccount(customer.getMasterAccount(), customer.getId());
            if(customerList.size() > 0){
                for(AppAccountNumberVO customer2 : customerList){
                    customer2.setSign("子账户");
                }
                map.put("subAccount", customerList);
            }
        }

        return ResponseServer.success("加载成功", map);
    }


    /**
     * 判断100/200专区参团时间
     * @return
     */
    public boolean judgeJoinCollageTime() throws ParseException {
        boolean temp = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获得当天年月日
        String time = sdf.format(new Date());
        // 早上十点
        String time10 = time + " 10:00:00";
        Date date10 = sdf1.parse(time10);
        Calendar begin10 = Calendar.getInstance();
        begin10.setTime(date10);
        // 晚上十点
        String time22 = time + " 22:00:00";
        Date date22 = sdf1.parse(time22);
        Calendar end22 = Calendar.getInstance();
        end22.setTime(date22);
        // 系统当前时间
        Date date = new Date();
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTime(date);
        // 是否可以参团
        if(nowTime.after(begin10) && nowTime.before(end22)){
            temp = true;
        }
        return temp;
    }


    /**
     * 判断用户是否已参与当前区-进行中
     * @param userId
     * @param productId
     * @return
     */
    public boolean judgeUserIsJoinCollage(Integer userId, Integer productId) {
        boolean temp = false;
        Product product = productDao.queryById(productId);
        List<Orders> ordersList = ordersDao.queryByModeIdAndUserId(product.getCollageModeId(), userId);
        if(ordersList.size() > 0){
            return temp;
        }else{
            temp = true;
            return temp;
        }
//        ModeProduct modeProduct = modeProductDao.queryByModeIdAndProductId(product.getCollageModeId(), product.getId());
//        if(modeProduct == null){
//            temp = true;
//            return temp;
//        }
//        CollageUser collageUser = collageUserDao.queryByUserIdAndModeProductId(userId, modeProduct.getId());
//        if(collageUser == null){
//            temp = true;
//            return temp;
//        }
//        return temp;
    }


    /**
     * 判断参团券是否充足 模式 = 30 40
     * @param userId
     * @param productId
     * @return
     */
    public boolean judgeCollageCouponEnough(Integer userId, Integer productId) {
        boolean temp = false;
        Product product = productDao.queryById(productId);
        UserCoupon userCoupon = userCouponDao.queryByUserIdAndCouponId(userId, product.getCouponId());
        if(userCoupon == null){
            UserCoupon newUc = new UserCoupon();
            newUc.setQuantity(0);
            newUc.setUpdateTime(new Date());
            newUc.setCouponId(product.getCouponId());
            newUc.setUserId(userId);
            userCouponDao.save(newUc);
        }else{
            if(userCoupon.getQuantity() > 0){
                temp = true;
            }
        }
        return temp;
    }


    /**
     * 判断极乐值是否充足 模式 != 30 40
     * @param request
     * @param productId
     * @return
     */
    @GetMapping("/judgeBlissEnough")
    public ResponseServer judgeBlissEnough(HttpServletRequest request, Integer productId) {
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        Product product = productDao.queryById(productId);
        Account account = accountDao.queryAccountByUserId(customer.getId());
        if(account.getBliss() >= product.getBliss()){
            return ResponseServer.success("可以参团");
        }
        return ResponseServer.error("极乐值不足!");
    }


    // 创建账户记录
    public void createAccountRecord(Integer userId, Date date, BigDecimal totalPrice, Account account, String outTradeNo, String agentCode){
        AccountRecord ar = new AccountRecord();
        ar.setOutUserId(userId);
        ar.setUpdateTime(date);
        ar.setType("0");
        ar.setAmount(totalPrice);
        ar.setNowAmount(account.getVoucherBalance());
        ar.setSign("2");
        ar.setOrderNo(outTradeNo);
        ar.setInfo(agentCode + "购买成功! 消费余额：" + totalPrice);
        accountRecordDao.save(ar);
    }


    // 创建订单数据
    public void createOrdersInfo(String outTradeNo, String tradeNo, BigDecimal totalPrice, BigDecimal freight, Product product, String collageModeName,
                                   Integer modeProductId, Integer userId, String consigneeName, String consigneePhone, String fullAddress, String isMember,
                                 SkuRelation sku, Integer buyQuantity, Date date, String payType, String prepayId, String assembleAmount){
        // 创建订单
        Orders orders = new Orders();
        orders.setOrderNo(outTradeNo);
        orders.setTradeNo(tradeNo);
        orders.setTotalAmount(totalPrice);

        orders.setCouponAmount(new BigDecimal(0));
        orders.setFreight(freight);
        orders.setPayType(payType);
        if("1".equals(payType)){
            orders.setPayAmount(totalPrice);
            orders.setPrepayId("");
            orders.setModeProductId(modeProductId);
            orders.setPayStatus("1");
            orders.setDealStatus("6"); // 等待开团
        }else if("4".equals(payType)){
            orders.setPayAmount(BigDecimal.valueOf(Double.parseDouble(assembleAmount)));
            orders.setPrepayId(prepayId);
            orders.setPayStatus("0");
            orders.setDealStatus("0"); // 待付款
        }else if("5".equals(payType)){
            orders.setPrepayId("");
            orders.setPayStatus("0");
            orders.setDealStatus("0"); // 待付款
            orders.setPayAmount(BigDecimal.valueOf(Double.parseDouble(assembleAmount)));
        }
        orders.setRefundStatus("0");
        orders.setCollageModeId(product.getCollageModeId());
        orders.setModeName(collageModeName);
        orders.setProductId(product.getId());
        orders.setOrderPersonId(userId);
        orders.setConsigneeName(consigneeName);
        orders.setConsigneePhone(consigneePhone);
        orders.setAddress(fullAddress);
        orders.setUpdateTime(date);
        orders.setIsDel("0");
        orders.setIsRun("0");
        if("0".equals(isMember)){
            orders.setLastCollage("0"); // 非会员 默认不拼中为止
        }else{
            orders.setLastCollage("1"); // 会员 默认拼中为止
        }
        orders.setRemarks("无");
        ordersDao.save(orders);
        // 订单详情
        OrderDetails od = new OrderDetails();
        od.setOrderNo(orders.getOrderNo());
        od.setTradeNo(orders.getTradeNo());
        od.setStoreShopId(product.getStoreShopId());
        od.setStoreShopName(storeShopDao.queryById(product.getStoreShopId()).getName());
        od.setClassifyId(product.getClassifyId());
        od.setClassifyName(classifyDao.queryById(product.getClassifyId()).getName());
        od.setProductId(product.getId());
        od.setProductType(product.getType());
        od.setProductImg(product.getImg());
        od.setProductName(product.getName());
        od.setProductCode(product.getProductCode());
        od.setCostPrice(sku.getCostPrice());
        od.setRelationId(sku.getId());
        if(sku.getGroupRelation().contains(",")){
            String[] strings = sku.getGroupRelation().split(",");
            String str = "";
            for(int u = 0; u < strings.length; u++){
                str += specAttributeDao.queryById(Integer.parseInt(strings[u])).getAttributeName() + "，";
            }
            od.setSpecAttributeName(str.substring(0, str.length()-1));
        }else{
            od.setSpecAttributeName(specAttributeDao.queryById(Integer.parseInt(sku.getGroupRelation())).getAttributeName());
        }
        od.setQuantity(buyQuantity);
        od.setUnitPrice(sku.getRetailPrice());
        od.setUpdateTime(date);
        orderDetailsDao.save(od);
    }


    /**
     * 挪订单，重新参团，100/200元专区的
     * @param o
     */
    public void handleTransferOrders(Orders o){
        OrdersCopy30 oc30 = new OrdersCopy30();
        oc30.setId(o.getId());
        oc30.setOrderNo(o.getOrderNo());
        oc30.setTradeNo(o.getTradeNo());
        oc30.setFreight(o.getFreight());
        oc30.setCollageModeId(o.getCollageModeId());
        oc30.setOrderPersonId(o.getOrderPersonId());
        oc30.setLastCollage(o.getLastCollage());
        oc30.setConsigneeName(o.getConsigneeName());
        oc30.setConsigneePhone(o.getConsigneePhone());
        oc30.setAddress(o.getAddress());
        oc30.setUpdateTime(o.getUpdateTime());
        oc30.setPayAmount(o.getPayAmount());
        oc30.setModeProductId(o.getModeProductId());
        oc30.setProductId(o.getProductId());
        oc30.setSkuId(orderDetailsDao.queryByOrderNo(o.getOrderNo()).getRelationId());
        ordersCopy30Dao.save(oc30);
    }


    /**
     * 统一加一个有效人数
     * @param customer
     */
    public void addEffectiveQuantity(Customer customer){
        if(customer.getParentId() != null && customer.getParentId() != 1198){
            Customer cu1 = customerDao.queryById(customer.getParentId());
            if(cu1 != null){
                cu1.setEffective(cu1.getEffective() + 1);
                customerDao.update(cu1);

                if(cu1.getParentId() != null && cu1.getParentId() != 1198){
                    Customer cu2 = customerDao.queryById(cu1.getParentId());
                    if(cu2 != null){
                        cu2.setEffective(cu2.getEffective() + 1);
                        customerDao.update(cu2);

                        if(cu2.getParentId() != null && cu2.getParentId() != 1198){
                            Customer cu3 = customerDao.queryById(cu2.getParentId());
                            if(cu3 != null){
                                cu3.setEffective(cu3.getEffective() + 1);
                                customerDao.update(cu3);

                                if(cu3.getParentId() != null && cu3.getParentId() != 1198){
                                    Customer cu4 = customerDao.queryById(cu3.getParentId());
                                    if(cu4 != null){
                                        cu4.setEffective(cu4.getEffective() + 1);
                                        customerDao.update(cu4);

                                        if(cu4.getParentId() != null && cu4.getParentId() != 1198){
                                            Customer cu5 = customerDao.queryById(cu4.getParentId());
                                            if(cu5 != null){
                                                cu5.setEffective(cu5.getEffective() + 1);
                                                customerDao.update(cu5);

                                                if(cu5.getParentId() != null && cu5.getParentId() != 1198){
                                                    Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                    if(cu6 != null){
                                                        cu6.setEffective(cu6.getEffective() + 1);
                                                        customerDao.update(cu6);

                                                        if(cu6.getParentId() != null && cu6.getParentId() != 1198){
                                                            Customer cu7 = customerDao.queryById(cu6.getParentId());
                                                            if(cu7 != null){
                                                                cu7.setEffective(cu7.getEffective() + 1);
                                                                customerDao.update(cu7);

                                                                if(cu7.getParentId() != null && cu7.getParentId() != 1198){
                                                                    Customer cu8 = customerDao.queryById(cu7.getParentId());
                                                                    if(cu8 != null){
                                                                        cu8.setEffective(cu8.getEffective() + 1);
                                                                        customerDao.update(cu8);

                                                                        if(cu8.getParentId() != null && cu8.getParentId() != 1198){
                                                                            Customer cu9 = customerDao.queryById(cu8.getParentId());
                                                                            if(cu9 != null){
                                                                                cu9.setEffective(cu9.getEffective() + 1);
                                                                                customerDao.update(cu9);

                                                                                if(cu9.getParentId() != null && cu9.getParentId() != 1198){
                                                                                    Customer cu10 = customerDao.queryById(cu9.getParentId());
                                                                                    if(cu10 != null){
                                                                                        cu10.setEffective(cu10.getEffective() + 1);
                                                                                        customerDao.update(cu10);

                                                                                        if(cu10.getParentId() != null && cu10.getParentId() != 1198){
                                                                                            Customer cu11 = customerDao.queryById(cu10.getParentId());
                                                                                            if(cu11 != null){
                                                                                                cu11.setEffective(cu11.getEffective() + 1);
                                                                                                customerDao.update(cu11);

                                                                                                if(cu11.getParentId() != null && cu11.getParentId() != 1198){
                                                                                                    Customer cu12 = customerDao.queryById(cu11.getParentId());
                                                                                                    if(cu12 != null){
                                                                                                        cu12.setEffective(cu12.getEffective() + 1);
                                                                                                        customerDao.update(cu12);

                                                                                                        if(cu12.getParentId() != null && cu12.getParentId() != 1198){
                                                                                                            Customer cu13 = customerDao.queryById(cu12.getParentId());
                                                                                                            if(cu13 != null){
                                                                                                                cu13.setEffective(cu13.getEffective() + 1);
                                                                                                                customerDao.update(cu13);

                                                                                                                if(cu13.getParentId() != null && cu13.getParentId() != 1198){
                                                                                                                    Customer cu14 = customerDao.queryById(cu13.getParentId());
                                                                                                                    if(cu14 != null){
                                                                                                                        cu14.setEffective(cu14.getEffective() + 1);
                                                                                                                        customerDao.update(cu14);

                                                                                                                        if(cu14.getParentId() != null && cu14.getParentId() != 1198){
                                                                                                                            Customer cu15 = customerDao.queryById(cu14.getParentId());
                                                                                                                            if(cu15 != null){
                                                                                                                                cu15.setEffective(cu15.getEffective() + 1);
                                                                                                                                customerDao.update(cu15);

                                                                                                                                if(cu15.getParentId() != null && cu15.getParentId() != 1198){
                                                                                                                                    Customer cu16 = customerDao.queryById(cu15.getParentId());
                                                                                                                                    if(cu16 != null){
                                                                                                                                        cu16.setEffective(cu16.getEffective() + 1);
                                                                                                                                        customerDao.update(cu16);

                                                                                                                                        if(cu16.getParentId() != null && cu16.getParentId() != 1198){
                                                                                                                                            Customer cu17 = customerDao.queryById(cu16.getParentId());
                                                                                                                                            if(cu17 != null){
                                                                                                                                                cu17.setEffective(cu17.getEffective() + 1);
                                                                                                                                                customerDao.update(cu17);

                                                                                                                                                if(cu17.getParentId() != null && cu17.getParentId() != 1198){
                                                                                                                                                    Customer cu18 = customerDao.queryById(cu17.getParentId());
                                                                                                                                                    if(cu18 != null){
                                                                                                                                                        cu18.setEffective(cu18.getEffective() + 1);
                                                                                                                                                        customerDao.update(cu18);

                                                                                                                                                        if(cu18.getParentId() != null && cu18.getParentId() != 1198){
                                                                                                                                                            Customer cu19 = customerDao.queryById(cu18.getParentId());
                                                                                                                                                            if(cu19 != null){
                                                                                                                                                                cu19.setEffective(cu19.getEffective() + 1);
                                                                                                                                                                customerDao.update(cu19);

                                                                                                                                                                if(cu19.getParentId() != null && cu19.getParentId() != 1198){
                                                                                                                                                                    Customer cu20 = customerDao.queryById(cu19.getParentId());
                                                                                                                                                                    if(cu20 != null){
                                                                                                                                                                        cu20.setEffective(cu20.getEffective() + 1);
                                                                                                                                                                        customerDao.update(cu20);

                                                                                                                                                                        if(cu20.getParentId() != null && cu20.getParentId() != 1198){
                                                                                                                                                                            Customer cu21 = customerDao.queryById(cu20.getParentId());
                                                                                                                                                                            if(cu21 != null){
                                                                                                                                                                                cu21.setEffective(cu21.getEffective() + 1);
                                                                                                                                                                                customerDao.update(cu21);

                                                                                                                                                                                if(cu21.getParentId() != null && cu21.getParentId() != 1198){
                                                                                                                                                                                    Customer cu22 = customerDao.queryById(cu21.getParentId());
                                                                                                                                                                                    if(cu22 != null){
                                                                                                                                                                                        cu22.setEffective(cu22.getEffective() + 1);
                                                                                                                                                                                        customerDao.update(cu22);

                                                                                                                                                                                        if(cu22.getParentId() != null && cu22.getParentId() != 1198){
                                                                                                                                                                                            Customer cu23 = customerDao.queryById(cu22.getParentId());
                                                                                                                                                                                            if(cu23 != null){
                                                                                                                                                                                                cu23.setEffective(cu23.getEffective() + 1);
                                                                                                                                                                                                customerDao.update(cu23);

                                                                                                                                                                                                if(cu23.getParentId() != null && cu23.getParentId() != 1198){
                                                                                                                                                                                                    Customer cu24 = customerDao.queryById(cu23.getParentId());
                                                                                                                                                                                                    if(cu24 != null){
                                                                                                                                                                                                        cu24.setEffective(cu24.getEffective() + 1);
                                                                                                                                                                                                        customerDao.update(cu24);

                                                                                                                                                                                                        if(cu24.getParentId() != null && cu24.getParentId() != 1198){
                                                                                                                                                                                                            Customer cu25 = customerDao.queryById(cu24.getParentId());
                                                                                                                                                                                                            if(cu25 != null){
                                                                                                                                                                                                                cu25.setEffective(cu25.getEffective() + 1);
                                                                                                                                                                                                                customerDao.update(cu25);

                                                                                                                                                                                                                if(cu25.getParentId() != null && cu25.getParentId() != 1198){
                                                                                                                                                                                                                    Customer cu26 = customerDao.queryById(cu25.getParentId());
                                                                                                                                                                                                                    if(cu26 != null){
                                                                                                                                                                                                                        cu26.setEffective(cu26.getEffective() + 1);
                                                                                                                                                                                                                        customerDao.update(cu26);

                                                                                                                                                                                                                        if(cu26.getParentId() != null && cu26.getParentId() != 1198){
                                                                                                                                                                                                                            Customer cu27 = customerDao.queryById(cu26.getParentId());
                                                                                                                                                                                                                            if(cu27 != null){
                                                                                                                                                                                                                                cu27.setEffective(cu27.getEffective() + 1);
                                                                                                                                                                                                                                customerDao.update(cu27);

                                                                                                                                                                                                                                if(cu27.getParentId() != null && cu27.getParentId() != 1198){
                                                                                                                                                                                                                                    Customer cu28 = customerDao.queryById(cu27.getParentId());
                                                                                                                                                                                                                                    if(cu28 != null){
                                                                                                                                                                                                                                        cu28.setEffective(cu28.getEffective() + 1);
                                                                                                                                                                                                                                        customerDao.update(cu28);

                                                                                                                                                                                                                                        if(cu28.getParentId() != null && cu28.getParentId() != 1198){
                                                                                                                                                                                                                                            Customer cu29 = customerDao.queryById(cu28.getParentId());
                                                                                                                                                                                                                                            if(cu29 != null){
                                                                                                                                                                                                                                                cu29.setEffective(cu29.getEffective() + 1);
                                                                                                                                                                                                                                                customerDao.update(cu29);

                                                                                                                                                                                                                                                if(cu29.getParentId() != null && cu29.getParentId() != 1198){
                                                                                                                                                                                                                                                    Customer cu30 = customerDao.queryById(cu29.getParentId());
                                                                                                                                                                                                                                                    if(cu30 != null){
                                                                                                                                                                                                                                                        cu30.setEffective(cu30.getEffective() + 1);
                                                                                                                                                                                                                                                        customerDao.update(cu30);

                                                                                                                                                                                                                                                        if(cu30.getParentId() != null && cu30.getParentId() != 1198){
                                                                                                                                                                                                                                                            Customer cu31 = customerDao.queryById(cu30.getParentId());
                                                                                                                                                                                                                                                            if(cu31 != null){
                                                                                                                                                                                                                                                                cu31.setEffective(cu31.getEffective() + 1);
                                                                                                                                                                                                                                                                customerDao.update(cu31);

                                                                                                                                                                                                                                                                if(cu31.getParentId() != null && cu31.getParentId() != 1198){
                                                                                                                                                                                                                                                                    Customer cu32 = customerDao.queryById(cu31.getParentId());
                                                                                                                                                                                                                                                                    if(cu32 != null){
                                                                                                                                                                                                                                                                        cu32.setEffective(cu32.getEffective() + 1);
                                                                                                                                                                                                                                                                        customerDao.update(cu32);

                                                                                                                                                                                                                                                                        if(cu32.getParentId() != null && cu32.getParentId() != 1198){
                                                                                                                                                                                                                                                                            Customer cu33 = customerDao.queryById(cu32.getParentId());
                                                                                                                                                                                                                                                                            if(cu33 != null){
                                                                                                                                                                                                                                                                                cu33.setEffective(cu33.getEffective() + 1);
                                                                                                                                                                                                                                                                                customerDao.update(cu33);

                                                                                                                                                                                                                                                                                if(cu33.getParentId() != null && cu33.getParentId() != 1198){
                                                                                                                                                                                                                                                                                    Customer cu34 = customerDao.queryById(cu33.getParentId());
                                                                                                                                                                                                                                                                                    if(cu34 != null){
                                                                                                                                                                                                                                                                                        cu34.setEffective(cu34.getEffective() + 1);
                                                                                                                                                                                                                                                                                        customerDao.update(cu34);

                                                                                                                                                                                                                                                                                        if(cu34.getParentId() != null && cu34.getParentId() != 1198){
                                                                                                                                                                                                                                                                                            Customer cu35 = customerDao.queryById(cu34.getParentId());
                                                                                                                                                                                                                                                                                            if(cu35 != null){
                                                                                                                                                                                                                                                                                                cu35.setEffective(cu35.getEffective() + 1);
                                                                                                                                                                                                                                                                                                customerDao.update(cu35);

                                                                                                                                                                                                                                                                                                if(cu35.getParentId() != null && cu35.getParentId() != 1198){
                                                                                                                                                                                                                                                                                                    Customer cu36 = customerDao.queryById(cu35.getParentId());
                                                                                                                                                                                                                                                                                                    if(cu36 != null){
                                                                                                                                                                                                                                                                                                        cu36.setEffective(cu36.getEffective() + 1);
                                                                                                                                                                                                                                                                                                        customerDao.update(cu36);

                                                                                                                                                                                                                                                                                                        if(cu36.getParentId() != null && cu36.getParentId() != 1198){
                                                                                                                                                                                                                                                                                                            Customer cu37 = customerDao.queryById(cu36.getParentId());
                                                                                                                                                                                                                                                                                                            if(cu37 != null){
                                                                                                                                                                                                                                                                                                                cu37.setEffective(cu37.getEffective() + 1);
                                                                                                                                                                                                                                                                                                                customerDao.update(cu37);

                                                                                                                                                                                                                                                                                                                if(cu37.getParentId() != null && cu37.getParentId() != 1198){
                                                                                                                                                                                                                                                                                                                    Customer cu38 = customerDao.queryById(cu37.getParentId());
                                                                                                                                                                                                                                                                                                                    if(cu38 != null){
                                                                                                                                                                                                                                                                                                                        cu38.setEffective(cu38.getEffective() + 1);
                                                                                                                                                                                                                                                                                                                        customerDao.update(cu38);

                                                                                                                                                                                                                                                                                                                        if(cu38.getParentId() != null && cu38.getParentId() != 1198){
                                                                                                                                                                                                                                                                                                                            Customer cu39 = customerDao.queryById(cu38.getParentId());
                                                                                                                                                                                                                                                                                                                            if(cu39 != null){
                                                                                                                                                                                                                                                                                                                                cu39.setEffective(cu39.getEffective() + 1);
                                                                                                                                                                                                                                                                                                                                customerDao.update(cu39);

                                                                                                                                                                                                                                                                                                                                if(cu39.getParentId() != null && cu39.getParentId() != 1198){
                                                                                                                                                                                                                                                                                                                                    Customer cu40 = customerDao.queryById(cu39.getParentId());
                                                                                                                                                                                                                                                                                                                                    if(cu40 != null){
                                                                                                                                                                                                                                                                                                                                        cu40.setEffective(cu40.getEffective() + 1);
                                                                                                                                                                                                                                                                                                                                        customerDao.update(cu40);

                                                                                                                                                                                                                                                                                                                                        if(cu40.getParentId() != null && cu40.getParentId() != 1198){
                                                                                                                                                                                                                                                                                                                                            Customer cu41 = customerDao.queryById(cu40.getParentId());
                                                                                                                                                                                                                                                                                                                                            if(cu41 != null){
                                                                                                                                                                                                                                                                                                                                                cu41.setEffective(cu41.getEffective() + 1);
                                                                                                                                                                                                                                                                                                                                                customerDao.update(cu41);

                                                                                                                                                                                                                                                                                                                                                if(cu41.getParentId() != null && cu41.getParentId() != 1198){
                                                                                                                                                                                                                                                                                                                                                    Customer cu42 = customerDao.queryById(cu41.getParentId());
                                                                                                                                                                                                                                                                                                                                                    if(cu42 != null){
                                                                                                                                                                                                                                                                                                                                                        cu42.setEffective(cu42.getEffective() + 1);
                                                                                                                                                                                                                                                                                                                                                        customerDao.update(cu42);
                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                }
                                                                                                                                                                                                                            }
                                                                                                                                                                                                                        }
                                                                                                                                                                                                                    }
                                                                                                                                                                                                                }
                                                                                                                                                                                                            }
                                                                                                                                                                                                        }
                                                                                                                                                                                                    }
                                                                                                                                                                                                }
                                                                                                                                                                                            }
                                                                                                                                                                                        }
                                                                                                                                                                                    }
                                                                                                                                                                                }
                                                                                                                                                                            }
                                                                                                                                                                        }
                                                                                                                                                                    }
                                                                                                                                                                }
                                                                                                                                                            }
                                                                                                                                                        }
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }


    /**
     * 处理参与拼团后的等级
     * @param customer
     */
    public void handleCollageLevel(Customer customer){
        Integer levelId = customer.getLevelId();
        if(customer.getLevelId() == 1){ //游客
            customer.setLevelId(2); // 升幼蜂
            if("0".equals(customer.getIsActive())){
                customer.setIsActive("1"); // 激活拼团等级
            }
            customerDao.update(customer);
            if(customer.getParentId() != null && customer.getParentId() != 1198 && customer.getLevelId() != 7){
                Customer parentCustomer = customerDao.queryById(customer.getParentId());
                if(parentCustomer != null){
                    if(parentCustomer.getLevelId() == 1){ // 游客

                    }else if(parentCustomer.getLevelId() == 2){ // 会员
                        // 判断是否直推5个幼蜂同时团队20人
                        int count = customerDao.queryCustomerByLevelIdAndParentId(2, parentCustomer.getId());
                        //int count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, parentCustomer.getId());
                        if(count >= 5){
                            parentCustomer.setLevelId(3); // 升小蜜蜂
                            customerDao.update(parentCustomer);
                            if(parentCustomer.getParentId() != null && parentCustomer.getParentId() != 1198 && parentCustomer.getLevelId() != 7){
                                Customer top3Customer = customerDao.queryById(parentCustomer.getParentId());
                                if(top3Customer != null){
                                    if(top3Customer.getLevelId() == 1){

                                    }else if(top3Customer.getLevelId() == 2){

                                    }else if(top3Customer.getLevelId() == 3){
                                        // 判断是否直推3个小蜜蜂同时团队100人
                                        int top3Count = customerDao.queryCustomerByLevelIdAndParentId(3, top3Customer.getId());
                                        //int top3Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top3Customer.getId());
                                        if(top3Count >= 3 && top3Customer.getEffective() >= 100) {
                                            top3Customer.setLevelId(4); // 升黄蜂
                                            customerDao.update(top3Customer);
                                            if(top3Customer.getParentId() != null && top3Customer.getParentId() != 1198 && top3Customer.getLevelId() != 7){
                                                Customer top4Customer = customerDao.queryById(top3Customer.getParentId());
                                                if(top4Customer != null){
                                                    if(top4Customer.getLevelId() == 1){

                                                    }else if(top4Customer.getLevelId() == 2){

                                                    }else if(top4Customer.getLevelId() == 3){

                                                    }else if(top4Customer.getLevelId() == 4){
                                                        // 判断是否直推5个黄蜂同时团队1000人
                                                        int top4Count = customerDao.queryCustomerByLevelIdAndParentId(4, top4Customer.getId());
                                                        //int top4Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top4Customer.getId());
                                                        if(top4Count >= 5 && top4Customer.getEffective() >= 1000) {
                                                            top4Customer.setLevelId(5); // 升蜂王
                                                            customerDao.update(top4Customer);
                                                            if(top4Customer.getParentId() != null && top4Customer.getParentId() != 1198 && top4Customer.getLevelId() != 7){
                                                                Customer top5Customer = customerDao.queryById(top4Customer.getParentId());
                                                                if(top5Customer != null){
                                                                    if(top5Customer.getLevelId() == 1){

                                                                    }else if(top5Customer.getLevelId() == 2){

                                                                    }else if(top5Customer.getLevelId() == 3){

                                                                    }else if(top5Customer.getLevelId() == 4){

                                                                    }else if(top5Customer.getLevelId() == 5){
                                                                        // 判断是否直推3个蜂王同时团队3000人
                                                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top5Customer.getId());
                                                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top5Customer.getId());
                                                                        if(top5Count >= 3 && top5Customer.getEffective() >= 3000) {
                                                                            top5Customer.setLevelId(6); // 升蜂皇
                                                                            customerDao.update(top5Customer);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }else if(top4Customer.getLevelId() == 5){
                                                        // 判断是否直推3个蜂王同时团队3000人
                                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top4Customer.getId());
                                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top4Customer.getId());
                                                        if(top5Count >= 3 && top4Customer.getEffective() >= 3000) {
                                                            top4Customer.setLevelId(6); // 升蜂皇
                                                            customerDao.update(top4Customer);
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                    }else if(top3Customer.getLevelId() == 4){
                                        // 判断是否直推5个黄蜂同时团队1000人
                                        int top4Count = customerDao.queryCustomerByLevelIdAndParentId(4, top3Customer.getId());
                                        //int top4Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top3Customer.getId());
                                        if(top4Count >= 5 && top3Customer.getEffective() >= 1000) {
                                            top3Customer.setLevelId(5); // 升蜂王
                                            customerDao.update(top3Customer);
                                            if(top3Customer.getParentId() != null && top3Customer.getParentId() != 1198 && top3Customer.getLevelId() != 7){
                                                Customer top5Customer = customerDao.queryById(top3Customer.getParentId());
                                                if(top5Customer != null){
                                                    if(top5Customer.getLevelId() == 1){

                                                    }else if(top5Customer.getLevelId() == 2){

                                                    }else if(top5Customer.getLevelId() == 3){

                                                    }else if(top5Customer.getLevelId() == 4){

                                                    }else if(top5Customer.getLevelId() == 5){
                                                        // 判断是否直推3个蜂王同时团队3000人
                                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top5Customer.getId());
                                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top5Customer.getId());
                                                        if(top5Count >= 3 && top5Customer.getEffective() >= 3000) {
                                                            top5Customer.setLevelId(6); // 升蜂皇
                                                            customerDao.update(top5Customer);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }else if(top3Customer.getLevelId() == 5){
                                        // 判断是否直推3个蜂王同时团队3000人
                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top3Customer.getId());
                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top3Customer.getId());
                                        if(top5Count >= 3 && top3Customer.getEffective() >= 3000) {
                                            top3Customer.setLevelId(6); // 升蜂皇
                                            customerDao.update(top3Customer);
                                        }
                                    }
                                }
                            }
                        }

                    }else if(parentCustomer.getLevelId() == 3){ // 小蜜蜂
                        // 判断是否直推3个小蜜蜂同时团队100人
                        int top3Count = customerDao.queryCustomerByLevelIdAndParentId(3, parentCustomer.getId());
                        //int top3Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, parentCustomer.getId());
                        if(top3Count >= 3 && parentCustomer.getEffective() >= 100) {
                            parentCustomer.setLevelId(4); // 升黄蜂
                            customerDao.update(parentCustomer);
                            if(parentCustomer.getParentId() != null && parentCustomer.getParentId() != 1198 && parentCustomer.getLevelId() != 7){
                                Customer top4Customer = customerDao.queryById(parentCustomer.getParentId());
                                if(top4Customer != null){
                                    if(top4Customer.getLevelId() == 1){

                                    }else if(top4Customer.getLevelId() == 2){

                                    }else if(top4Customer.getLevelId() == 3){

                                    }else if(top4Customer.getLevelId() == 4){
                                        // 判断是否直推5个黄蜂同时团队1000人
                                        int top4Count = customerDao.queryCustomerByLevelIdAndParentId(4, top4Customer.getId());
                                        //int top4Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top4Customer.getId());
                                        if(top4Count >= 5 && top4Customer.getEffective() >= 1000) {
                                            top4Customer.setLevelId(5); // 升蜂王
                                            customerDao.update(top4Customer);
                                            if(top4Customer.getParentId() != null && top4Customer.getParentId() != 1198 && top4Customer.getLevelId() != 7){
                                                Customer top5Customer = customerDao.queryById(top4Customer.getParentId());
                                                if(top5Customer != null){
                                                    if(top5Customer.getLevelId() == 1){

                                                    }else if(top5Customer.getLevelId() == 2){

                                                    }else if(top5Customer.getLevelId() == 3){

                                                    }else if(top5Customer.getLevelId() == 4){

                                                    }else if(top5Customer.getLevelId() == 5){
                                                        // 判断是否直推3个蜂王同时团队3000人
                                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top5Customer.getId());
                                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top5Customer.getId());
                                                        if(top5Count >= 3 && top5Customer.getEffective() >= 3000) {
                                                            top5Customer.setLevelId(6); // 升蜂皇
                                                            customerDao.update(top5Customer);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }else if(top4Customer.getLevelId() == 5){
                                        // 判断是否直推3个蜂王同时团队3000人
                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top4Customer.getId());
                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top4Customer.getId());
                                        if(top5Count >= 3 && top4Customer.getEffective() >= 3000) {
                                            top4Customer.setLevelId(6); // 升蜂皇
                                            customerDao.update(top4Customer);
                                        }
                                    }
                                }
                            }
                        }
                    }else if(parentCustomer.getLevelId() == 4){ // 黄蜂
                        // 判断是否直推5个黄蜂同时团队1000人
                        int top4Count = customerDao.queryCustomerByLevelIdAndParentId(4, parentCustomer.getId());
                        //int top4Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, parentCustomer.getId());
                        if(top4Count >= 5 && parentCustomer.getEffective() >= 1000) {
                            parentCustomer.setLevelId(5); // 升蜂王
                            customerDao.update(parentCustomer);
                            if(parentCustomer.getParentId() != null && parentCustomer.getParentId() != 1198 && parentCustomer.getLevelId() != 7){
                                Customer top5Customer = customerDao.queryById(parentCustomer.getParentId());
                                if(top5Customer != null){
                                    if(top5Customer.getLevelId() == 1){

                                    }else if(top5Customer.getLevelId() == 2){

                                    }else if(top5Customer.getLevelId() == 3){

                                    }else if(top5Customer.getLevelId() == 4){

                                    }else if(top5Customer.getLevelId() == 5){
                                        // 判断是否直推3个蜂王同时团队3000人
                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top5Customer.getId());
                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top5Customer.getId());
                                        if(top5Count >= 3 && top5Customer.getEffective() >= 3000) {
                                            top5Customer.setLevelId(6); // 升蜂皇
                                            customerDao.update(top5Customer);
                                        }
                                    }
                                }
                            }
                        }
                    }else if(parentCustomer.getLevelId() == 5){ // 蜂王
                        // 判断是否直推3个蜂王同时团队3000人
                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, parentCustomer.getId());
                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, parentCustomer.getId());
                        if(top5Count >= 3 && parentCustomer.getEffective() >= 3000) {
                            parentCustomer.setLevelId(6); // 升蜂皇
                            customerDao.update(parentCustomer);
                        }
                    }
                }
            }

        }else if(customer.getLevelId() == 2){ // 幼蜂

        }else if(customer.getLevelId() == 3){ // 小蜜蜂

        }else if(customer.getLevelId() == 4){ // 黄蜂

        }else if(customer.getLevelId() == 5){ // 蜂王

        }else if(customer.getLevelId() == 6){ // 蜂皇

        }else if(customer.getLevelId() == 7){ // 公司

        }

        // 添加有效人数
        if(levelId == 1){
            addEffectiveQuantity(customer);
        }else if(levelId == 4 || levelId == 5 || levelId == 6){
            List<Orders> ordersList = ordersDao.queryOrdersBy30Or40(customer.getId());
            if(ordersList.size() < 1){
                addEffectiveQuantity(customer);
            }
        }

    }


    /**
     * 参与拼团，订单支付
     * @param request
     * @param parameter  [payType] （0=微信支付，1=余额支付，2=积分支付，3=拼团余额支付，4=app微信支付，5=app支付宝支付，6=共享余额）
     * @return
     */
    @PostMapping("/order/pay/collage")
    public ResponseServer orderPayCollage(@RequestBody String parameter, HttpServletRequest request) throws Exception {
        System.err.println("参与拼团，订单支付********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }

        // 判断100/200专区参团时间
        boolean bool = judgeJoinCollageTime();
        if(!bool){
            if(customer.getId() == 1239){
                // 1239 不限制时间
            }else{
                return ResponseServer.error("每日参团时间为：10:00 - 22:00");
            }
        }

        JSONObject jsonObject = JSONObject.parseObject(parameter);
        Integer productId = (Integer) jsonObject.get("productId");
        Integer skuId = (Integer) jsonObject.get("skuId");
        Integer addressId = (Integer) jsonObject.get("addressId");
        String oldFreight = jsonObject.get("freight").toString();
        String payType = jsonObject.get("payType").toString();
        Integer buyQuantity = 1; // (Integer) jsonObject.get("buyQuantity");

        SkuRelation sku = skuRelationDao.queryById(skuId);
        if(sku == null){
            return ResponseServer.error("支付失败,产品SKU错误");
        }

        Address address = addressDao.queryById(addressId);
        if(address == null){
            return ResponseServer.error("支付失败,收货地址错误");
        }

        Product product = productDao.queryById(productId);
        if(product == null){
            return ResponseServer.error("支付失败,产品信息错误");
        }

        // 判断用户是否已参与当前团-进行中
        boolean bool1 = judgeUserIsJoinCollage(customer.getId(), product.getId());
        if(!bool1){
            return ResponseServer.error("不可重复参团!");
        }

        // 判断参团券是否充足 模式 = 30 40
        boolean bool0 = judgeCollageCouponEnough(customer.getId(), product.getId());
        if(!bool0){
            return ResponseServer.error("参团券不足! 请签到");
        }

        List<StoreShopFreightVO> ssfList = storeShopFreightDao.queryStoreShopFreightVOById(product.getStoreShopId());
        if(ssfList.size() > 0){
            for(StoreShopFreightVO ssf : ssfList){
                if(ssf.getChinaAreasName().equals(address.getProvince())){
                    if("0".equals(ssf.getStatus())){
                        return ResponseServer.error("该区域商家暂不配送");
                    }
                }
            }
        }

        Account account = accountDao.queryAccountByUserId(customer.getId());
        if(account == null){
            return ResponseServer.error("支付失败,账户信息错误,请联系管理员");
        }

        BigDecimal freight = BigDecimal.valueOf(Long.parseLong(oldFreight));
        if (freight == null) {
            freight = BigDecimal.valueOf(0.0);
        }

        CollageMode collageMode = collageModeDao.queryById(product.getCollageModeId());
        if(collageMode == null){
            return ResponseServer.error("支付失败,拼团模式信息错误,请联系管理员");
        }

        // 需要支付的总金额
        BigDecimal totalPrice = sku.getRetailPrice().multiply(BigDecimal.valueOf(buyQuantity)).add(freight);

        // 商户订单号
        String outTradeNo = "BEE" + Units.getOrderIdByTime().replaceAll("-", "");
        // 交易号
        String tradeNo = "JYH" + Units.getOrderIdByTime().replaceAll("-", "");

        Date date = new Date();

        Integer couponId_1 = 3; // 默认为30
        if(product.getCollageModeId() == 40){
            couponId_1 = 4; // 为40
        }
        if(product.getCollageModeId() == 30 || product.getCollageModeId() == 40){
            UserCoupon userCoupon = userCouponDao.queryByUserIdAndCouponId(customer.getId(), couponId_1);
            if(userCoupon.getQuantity() < 1){
                return ResponseServer.error("参团券不足! 请签到");
            }else{
                if("1".equals(payType)){
                    if(account.getVoucherBalance().doubleValue() < totalPrice.doubleValue()){
                        return ResponseServer.error("支付失败,余额不足");
                    }
                    account.setVoucherBalance(account.getVoucherBalance().subtract(totalPrice));
                    accountDao.update(account);

                    // 销量+1
                    product.setSales(product.getSales() + 1);
                    productDao.update(product);

                    if("0".equals(customer.getIsMember())){
                        // 扣除参团券1张
                        userCoupon.setQuantity(userCoupon.getQuantity() - 1);
                        userCouponDao.update(userCoupon);
                    }else if("1".equals(customer.getIsMember())){
                        // 扣除参团券15张
                        userCoupon.setQuantity(0);
                        userCouponDao.update(userCoupon);
                    }

                    // 创建账户记录
                    createAccountRecord(customer.getId(), date, totalPrice, account, outTradeNo, customer.getAgentCode());

                    // 处理团信息
                    ModeProduct modeProduct = modeProductDao.queryByProductIdByStatus(product.getId());
                    if(modeProduct == null){
                        // 创建新团
                        ModeProduct newMP = new ModeProduct();
                        newMP.setModeId(product.getCollageModeId());
                        newMP.setProductId(product.getId());
                        newMP.setStatus("0");
                        newMP.setUpdateTime(date);
                        modeProductDao.save(newMP);
                        // 加入到当前团
                        CollageUser newCU = new CollageUser();
                        newCU.setModeProductId(newMP.getId());
                        newCU.setUserId(customer.getId());
                        newCU.setUpdateTime(date);
                        collageUserDao.save(newCU);

                        // TODO 创建订单数据
                        createOrdersInfo(outTradeNo, tradeNo, totalPrice, freight, product, collageMode.getName(), newMP.getId(), customer.getId(),
                                address.getConsigneeName(), address.getConsigneePhone(), address.getFullAddress(), customer.getIsMember(), sku, buyQuantity, date, payType, "", "");

                    }else{
                        int quantity = collageMode.getPeopleQuantity(); // 20
                        // 查询现在这个产品团里面有多少人了
                        List<CollageUser> collageUserList = collageUserDao.queryByModeProductId(modeProduct.getId());
                        int rowSize = collageUserList.size(); // 已参与人数
                        if(rowSize < (quantity -1)){
                            // 直接加入
                            CollageUser newCU = new CollageUser();
                            newCU.setModeProductId(modeProduct.getId());
                            newCU.setUserId(customer.getId());
                            newCU.setUpdateTime(date);
                            collageUserDao.save(newCU);

                            // TODO 创建订单数据
                            createOrdersInfo(outTradeNo, tradeNo, totalPrice, freight, product, collageMode.getName(), modeProduct.getId(), customer.getId(),
                                    address.getConsigneeName(), address.getConsigneePhone(), address.getFullAddress(), customer.getIsMember(), sku, buyQuantity, date, payType, "", "");

                        }else if(rowSize == (quantity -1)){ // 需要重新生成新团
                            // 加入当前团
                            // 修改老团,拼团成功
                            modeProduct.setStatus("1");
                            modeProductDao.update(modeProduct);
                            CollageUser newCU = new CollageUser();
                            newCU.setModeProductId(modeProduct.getId());
                            newCU.setUserId(customer.getId());
                            newCU.setUpdateTime(date);
                            collageUserDao.save(newCU);
                            // 创建新团
                            ModeProduct newMo = new ModeProduct();
                            newMo.setUpdateTime(new Date());
                            newMo.setProductId(product.getId());
                            newMo.setModeId(collageMode.getId());
                            newMo.setStatus("0");
                            modeProductDao.save(newMo);

                            // TODO 创建订单数据
                            createOrdersInfo(outTradeNo, tradeNo, totalPrice, freight, product, collageMode.getName(), modeProduct.getId(), customer.getId(),
                                    address.getConsigneeName(), address.getConsigneePhone(), address.getFullAddress(), customer.getIsMember(), sku, buyQuantity, date, payType, "", "");

                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            String today = sdf.format(new Date());

                            // 获取当前团的订单信息，处理哪些需要发货，哪些需要退款，哪些需要挪团
                            List<Orders> ordersList = ordersDao.queryOrdersByCollageProductId(modeProduct.getId());
                            int ordersSize = ordersList.size();
                            if(ordersSize > 0){
                                for(int u = 0; u < ordersSize; u++) {
                                    Orders o = ordersList.get(u);
                                    Customer cu = customerDao.queryById(o.getOrderPersonId());
                                    OrderDetails od = orderDetailsDao.queryByOrderNo(o.getOrderNo());
                                    Product p = productDao.queryById(od.getProductId());
                                    if ("1".equals(cu.getIsMember())) {
                                        // 会员今日每中 直接一波流出局
                                        // todo 那29元 修改为14
                                        BigDecimal profit = BigDecimal.valueOf(14);
                                        if (p.getCollageModeId() == 40) {
                                            // todo 拿走58元 修改为28
                                            profit = BigDecimal.valueOf(28);
                                        }
                                        CollageTotalVoucher ctv = collageTotalVoucherDao.queryByIdsAndTodayTime(cu.getId(), p.getCollageModeId(), today);
                                        if (ctv == null) {
                                            CollageTotalVoucher newCtv = new CollageTotalVoucher();
                                            newCtv.setAmount(profit);
                                            newCtv.setModeId(p.getCollageModeId());
                                            newCtv.setStatus("0");
                                            newCtv.setUpdateTime(date);
                                            newCtv.setUserId(cu.getId());
                                            collageTotalVoucherDao.save(newCtv);
                                            // 分润
                                            ordersService.handleCollageProfit30(cu.getId(), profit, o.getOrderNo());
                                        }

                                        // 获取累计中奖记录数据
                                        CollageInRecord30 cir30 = collageInRecord30Dao.queryByModeIdAndUserId(o.getCollageModeId(), cu.getId());
                                        if (cir30 == null) {
                                            // 创建cir30
                                            CollageInRecord30 newCir30 = new CollageInRecord30();
                                            newCir30.setModeId(o.getCollageModeId());
                                            newCir30.setUserId(cu.getId());
                                            newCir30.setStatus("0");
                                            newCir30.setTotalQuantity(1);
                                            newCir30.setUpdateTime(date);
                                            newCir30.setPayAmount(o.getPayAmount());
                                            collageInRecord30Dao.save(newCir30);
                                            // 添加详情
                                            CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                            newCir30Copy.setIsTack("1"); // 提了
                                            newCir30Copy.setCir30Id(newCir30.getId());
                                            newCir30Copy.setOrderNo(o.getOrderNo());
                                            newCir30Copy.setPayAmount(o.getPayAmount());
                                            collageInRecord30CopyDao.save(newCir30Copy);
                                            // 发货
                                            o.setDealStatus("1");
                                            ordersDao.update(o);
                                        } else {
                                            // 判断是否需要返钱
                                            if (cir30.getTotalQuantity() == 5) {
                                                // 开始退钱，退未提货的钱
                                                Account cuAcc = accountDao.queryAccountByUserId(cu.getId());
                                                Double d = 0.0;
                                                List<CollageInRecord30Copy> cir30CopyList = collageInRecord30CopyDao.queryByCir30Id(cir30.getId());
                                                if (cir30CopyList.size() > 0) {
                                                    for (CollageInRecord30Copy cir30Copy : cir30CopyList) {
                                                        cir30Copy.setIsTack("1"); // 修改为已提货
                                                        d += cir30Copy.getPayAmount().doubleValue();
                                                        collageInRecord30CopyDao.update(cir30Copy);
                                                        Orders orders = ordersDao.queryByOrderNo(cir30Copy.getOrderNo());
                                                        orders.setDealStatus("3"); // 已完成
                                                        orders.setConfirmTime(new Date());
                                                        orders.setRemarks("拼团专区内累计拼中六次! 当前订单自动完成");
                                                        ordersDao.update(orders);
                                                    }
                                                }
                                                cuAcc.setVoucherBalance(cuAcc.getVoucherBalance().add(BigDecimal.valueOf(d))); //余额
                                                accountDao.update(cuAcc);
                                                // 加个ar记录
                                                AccountRecord ar = new AccountRecord();
                                                ar.setIntoUserId(cu.getId());
                                                ar.setUpdateTime(date);
                                                ar.setType("0");
                                                ar.setAmount(BigDecimal.valueOf(d));
                                                ar.setNowAmount(cuAcc.getVoucherBalance());
                                                ar.setSign("1");
                                                ar.setOrderNo(o.getOrderNo());
                                                ar.setInfo("拼团专区内累计拼中六次! 广告补贴：" + d);
                                                accountRecordDao.save(ar);
                                                // 改为退完
                                                cir30.setStatus("1"); // 已退
                                                collageInRecord30Dao.update(cir30);
                                                // 把第六次做为新的第一次并发货
                                                // 发货
                                                o.setDealStatus("1");
                                                ordersDao.update(o);
                                                // 创建cir30
                                                CollageInRecord30 newCir30 = new CollageInRecord30();
                                                newCir30.setModeId(o.getCollageModeId());
                                                newCir30.setUserId(cu.getId());
                                                newCir30.setStatus("0");
                                                newCir30.setTotalQuantity(1);
                                                newCir30.setUpdateTime(date);
                                                newCir30.setPayAmount(o.getPayAmount());
                                                collageInRecord30Dao.save(newCir30);
                                                // 添加详情
                                                CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                                newCir30Copy.setIsTack("1"); // 提了
                                                newCir30Copy.setCir30Id(newCir30.getId());
                                                newCir30Copy.setOrderNo(o.getOrderNo());
                                                newCir30Copy.setPayAmount(o.getPayAmount());
                                                collageInRecord30CopyDao.save(newCir30Copy);
                                            } else {
                                                // 继续累计
                                                cir30.setTotalQuantity(cir30.getTotalQuantity() + 1);
                                                cir30.setPayAmount(cir30.getPayAmount().add(o.getPayAmount()));
                                                collageInRecord30Dao.update(cir30);
                                                // 添加详情
                                                CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                                newCir30Copy.setIsTack("0"); // 没有提
                                                newCir30Copy.setCir30Id(cir30.getId());
                                                newCir30Copy.setOrderNo(o.getOrderNo());
                                                newCir30Copy.setPayAmount(o.getPayAmount());
                                                collageInRecord30CopyDao.save(newCir30Copy);
                                                // 代售
                                                o.setDealStatus("7");
                                                ordersDao.update(o);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // 处理升级
                    handleCollageLevel(customer);

                    return ResponseServer.success("拼团余额, 支付成功");

                }else if("4".equals(payType)){
                    System.err.println("微信支付 =========================== >>>>>>>>");
                    // 处理一下支付的金额
                    Random rn = new Random();
                    String oldAmount = String.valueOf(totalPrice);
                    String[] strings = oldAmount.split("\\.");
                    int a = rn.nextInt(8 - 6 + 1) + 6;
                    int s = rn.nextInt(9) + 1;
                    String assembleAmount = strings[0] + "." + a + s;
                    Map<String, Object> wxpay = new HashMap<>();
                    wxpay.put("totalPrice", assembleAmount);
                    wxpay.put("outTradeNo", outTradeNo);
                    //wxpay.put("openId", openid);
                    wxpay.put("notifyUrl", Constants.wxPay_notify_url_collage);
                    wxpay.put("wxPayAppId", Constants.app_appid);
                    wxpay.put("tradeType", Constants.app_trade_type);
                    String paramBuffer = CreateUnifiedOrder.UnifiedOrder(request, wxpay);
                    System.err.println("微信支付 =========================== >>>>>>>> paramBuffer: " + paramBuffer);
                    // 发送请求(POST)(获得数据包ID)(这有个注意的地方 如果不转码成ISO8859-1则会告诉你body不是UTF8编码
                    // 就算你改成UTF8编码也一样不好使 所以修改成ISO8859-1)
                    // String result = PayUtil.httpRequest(Constants.pay_url, "POST",
                    // xml);
                    Map<String, String> map = Units.doXMLParse(HttpRequestUtil.sendPost(Constants.pay_url,
                            new String(paramBuffer.getBytes(), "UTF-8")));
                    System.err.println("微信支付 =========================== >>>>>>>>map: " + map);
                    // 应该创建 支付表数据
                    if (map != null) {
                        String prepayId = map.get("prepay_id");

                        // TODO 创建订单数据
                        createOrdersInfo(outTradeNo, tradeNo, totalPrice, freight, product, collageMode.getName(), null, customer.getId(),
                                address.getConsigneeName(), address.getConsigneePhone(), address.getFullAddress(), customer.getIsMember(), sku, buyQuantity, date, payType, prepayId, assembleAmount);

                        net.sf.json.JSONObject resultJson = CreateUnifiedOrder.generateSignature(prepayId, Constants.app_appid);
                        resultJson.put("prepayId", prepayId);
                        resultJson.put("package", "Sign=WXPay");
                        // 设置openId条件
                        System.err.println("resultJson-----------------"+resultJson);
                        return ResponseServer.success(resultJson);
                    } else {
                        return ResponseServer.error("微信调取失败");
                    }
                }else if("5".equals(payType)){
                    // 处理一下支付的金额
                    Random rn = new Random();
                    String oldAmount = String.valueOf(totalPrice);
                    String[] strings = oldAmount.split("\\.");
                    int a = rn.nextInt(8 - 6 + 1) + 6;
                    int s = rn.nextInt(9) + 1;
                    String assembleAmount = strings[0] + "." + a + s;
                    Map<String, Object> aliPay = new HashMap<>();
                    aliPay.put("totalFee", String.valueOf(totalPrice));
                    aliPay.put("outTradeNo", outTradeNo);
                    aliPay.put("body", "生活用品");
                    ResponseServer responseServer = AliPayUtil.aliPaymentCollage(aliPay);
                    if (responseServer.getData() != null) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("form", responseServer.getData());

                        // TODO 创建订单数据
                        createOrdersInfo(outTradeNo, tradeNo, totalPrice, freight, product, collageMode.getName(), null, customer.getId(),
                                address.getConsigneeName(), address.getConsigneePhone(), address.getFullAddress(), customer.getIsMember(), sku, buyQuantity, date, payType, "-1", assembleAmount);

                        return ResponseServer.success(map);
                    }else{
                        return ResponseServer.error("调用支付宝支付失败");
                    }
                }else{
                    return ResponseServer.error("支付失败, 支付方式错误");
                }
            }
        }else{
            return ResponseServer.error("支付失败,请联系管理员");
        }

    }


    /**
     * 参与拼团，订单支付 - 微信回调
     * @param request
     * @param response
     */
    @PostMapping("/wxPay_notify_url_collage")
    public void wxPayCollagePayCallback(HttpServletRequest request, HttpServletResponse response) {
        String inputLine;
        String notifyXml = "";
        try {
            while ((inputLine = request.getReader().readLine()) != null) {
                notifyXml += inputLine;
            }
            // 关闭流
            request.getReader().close();
            // 解析成map
            Map<String, String> map = Units.doXMLParse(notifyXml);
            System.err.println("微信支付回调map ====================== >>>> " + map);
            // 判断支付是否成功
            if ("SUCCESS".equals(map.get("result_code"))) {
                System.err.println("微信回调返回是否支付成功：是");
                //获得返回的商户订单号
                String outTradeNo = map.get("out_trade_no");
                System.err.println("微信回调返回商户订单号：" + outTradeNo);

                Orders transmitOrders = ordersDao.queryByOrderNo(outTradeNo);
                if("0".equals(transmitOrders.getDealStatus())) {
                    transmitOrders.setPayStatus("1"); // 支付成功
                    transmitOrders.setDealStatus("6"); // 待开团
                    ordersDao.update(transmitOrders);

                    Product product = productDao.queryById(transmitOrders.getProductId());
                    product.setSales(product.getSales() + 1); // 添加销量
                    productDao.update(product);

                    // 处理团
                    ordersCopy30Service.handleAgainOrders_wxPay(transmitOrders);
                }

                System.err.println("微信支付回调处理完成");
                //封装 返回值
                StringBuffer buffer = new StringBuffer();
                buffer.append("<xml>");
                buffer.append("<return_code>SUCCESS</return_code>");
                buffer.append("<return_msg>OK</return_msg>");
                buffer.append("</xml>");
                //给微信服务器返回 成功标示 否则会一直询问 咱们服务器 是否回调成功
                PrintWriter writer = response.getWriter();
                //返回
                writer.print(buffer.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 参与拼团，订单支付 - 支付宝回调
     * @param request
     * @param response
     * @param modelMap
     * @throws Exception
     */
    @PostMapping("/aliPay_notify_url_collage")
    public void aliPayCollagePayCallback(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
        System.err.println("支付宝支付回调---------- modelMap ---------->>>> " + modelMap);
        // 获取支付宝POST过来反馈信息
        String result = "false";
        try {
            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
                String name = iter.next();
                String[] values = requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i]
                            : valueStr + values[i] + ",";
                }
                // 乱码解决
                valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
                params.put(name, valueStr);
            }
            System.err.println("支付宝回调信息：" + params);
            // 调用SDK验证签名
            boolean signVerified = AlipaySignature.rsaCheckV1(params, Constants.aliPay_public_key, Constants.aliPay_charset, Constants.aliPay_sign_type);

    	    /* 实际验证过程建议商户务必添加以下校验：
    		1、需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号，
    		2、判断total_amount是否确实为该订单的实际金额（即商户订单创建时的金额），
    		3、校验通知中的seller_id（或者seller_email) 是否为out_trade_no这笔单据的对应的操作方（有的时候，一个商户可能有多个seller_id/seller_email）
    		4、验证app_id是否为该商户本身。*/

            System.err.println("验证签名:" + signVerified);
            result = "success";
            // 商户订单号
            String outTradeNo = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
            System.err.println("订单号：" + outTradeNo);
            String total_amount = request.getParameter("total_amount"); // 获取总金额
            System.err.println("订单金额：" + total_amount);
            // 支付宝交易号
            String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
            System.err.println("支付宝交易号：" + trade_no);
            //交易状态
            String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");
            System.err.println("交易状态：" + trade_status);
            if (trade_status.equals("TRADE_SUCCESS")) {
                System.err.println("TRADE_SUCCESS");
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序
                //注意：
                //退款日期超过可退款期限后（如三个月可退款），支付宝系统发送该交易状态通知
                System.err.println("支付宝回调返回是否支付成功：是");

                Orders transmitOrders = ordersDao.queryByOrderNo(outTradeNo);
                if("0".equals(transmitOrders.getDealStatus())){
                    transmitOrders.setPayStatus("1"); // 支付成功
                    transmitOrders.setDealStatus("6"); // 待开团
                    ordersDao.update(transmitOrders);

                    Product product = productDao.queryById(transmitOrders.getProductId());
                    product.setSales(product.getSales() + 1); // 添加销量
                    productDao.update(product);

                    // 处理团
                    ordersCopy30Service.handleAgainOrders_wxPay(transmitOrders);
                }

                System.err.println("支付宝支付回调处理完成");
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=" + Constants.aliPay_charset);
        response.setCharacterEncoding(Constants.aliPay_charset);
        response.getWriter().write(result); // 直接将完整的表单html输出到页面
        response.getWriter().flush();
        response.getWriter().close();
        return;
    }


    /**
     * 代售订单中提货
     * @param parameter
     * @return
     */
    @PostMapping("/takeDeliveryGoods")
    public ResponseServer takeDeliveryGoods(@RequestBody String parameter){
        System.err.println("代售订单中提货********** parameter ---------- : " + parameter);
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String orderNo = jsonObject.get("orderNo").toString();
        Orders orders = ordersDao.queryByOrderNo(orderNo);
        if(orders == null){
            return ResponseServer.error("提货失败! 请核对订单数据或联系管理员");
        }
//        if(orders.getCollageModeId() == 30 || orders.getCollageModeId() == 40){
//            orders.setDealStatus("1");
//            ordersDao.update(orders);
//            CollageInRecord30Copy cir30Copy = collageInRecord30CopyDao.queryByOrderNo(orderNo);
//            if(cir30Copy == null){
//                return ResponseServer.error("提货失败! 请核对订单数据或联系管理员");
//            }
//            cir30Copy.setIsTack("1");
//            collageInRecord30CopyDao.update(cir30Copy);
//            CollageInRecord30 cir30 = collageInRecord30Dao.queryById(cir30Copy.getCir30Id());
//            if(cir30 == null){
//                return ResponseServer.error("提货失败! 请核对订单数据或联系管理员");
//            }
//            cir30.setTotalQuantity(cir30.getTotalQuantity() - 1);
//            cir30.setPayAmount(cir30.getPayAmount().subtract(orders.getPayAmount()));
//            collageInRecord30Dao.update(cir30);
//            return ResponseServer.success("提货成功!");
//        }else{
//
//            return ResponseServer.success("这个是喜欢就拼的代售");
//        }
        return ResponseServer.success("暂不支持提货!");
    }


    /**
     * 中途取消支付，直接删除订单
     * @param parameter
     * @return
     */
    @PostMapping("/cancelPayment")
    public ResponseServer cancelPayment(@RequestBody String parameter){
        System.err.println("中途取消支付，直接删除订单********** parameter ---------- : " + parameter);
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String orderNo = jsonObject.get("orderNo").toString();
        OrderDetails orderDetails = orderDetailsDao.queryByOrderNo(orderNo);
        orderDetailsDao.delete(orderDetails.getId());
        Orders orders = ordersDao.queryByOrderNo(orderNo);
        ordersDao.delete(orders.getId());
        return ResponseServer.success("已取消支付!");
    }


    /**
     * 加载首页数据
     * @return
     */
    @GetMapping("/loadingHomePageData")
    public ResponseServer loadingHomePageData(){
        Map map = new HashMap();

        // Banner
        List<Banners> bannersList = bannersDao.queryBannersByType("0");
        map.put("bannersList", bannersList);

        // Navigation
        List<AppHomePage> navigationList = appHomePageDao.queryByCondition();
        map.put("navigationList", navigationList);

        // Advert
        List<AppHomePageModeDTO> dtoList = new ArrayList<>();

        AppHomePageModeDTO dto = new AppHomePageModeDTO();
        dto.setId(1);
        dto.setImg("http://img.jiajiani.cn/1636380550849");
        dto.setIndexSort(1);
        dto.setName("这个长方形暂时不做跳转，图片展示而已");
        dto.setJumpType("0");
        dtoList.add(dto);

        map.put("advertList", dtoList);

        // Mode
        List<AppHomePageModeDTO> dtoList1 = new ArrayList<>();

        AppHomePageModeDTO dto1 = new AppHomePageModeDTO();
        dto1.setId(2);
        dto1.setImg("http://img.jiajiani.cn/1636381271769");
        dto1.setIndexSort(2);
        dto1.setName("共享专区");
        dto1.setJumpType("2");
        dto1.setParameter("0");
        dtoList1.add(dto1);

        AppHomePageModeDTO dto2 = new AppHomePageModeDTO();

        CollageMode cm30 = collageModeDao.queryById(30);
        dto2.setId(3);
        dto2.setImg(cm30.getImg());
        dto2.setIndexSort(3);
        dto2.setName(cm30.getName());
        dto2.setJumpType("8");
        dto2.setParameter("30");
        dtoList1.add(dto2);

        AppHomePageModeDTO dto3 = new AppHomePageModeDTO();
        CollageMode cm40 = collageModeDao.queryById(40);
        dto3.setId(4);
        dto3.setImg(cm40.getImg());
        dto3.setIndexSort(4);
        dto3.setName(cm40.getName());
        dto3.setJumpType("8");
        dto3.setParameter("40");
        dtoList1.add(dto3);

        map.put("modeList", dtoList1);

        return ResponseServer.success("加载首页数据成功!", map);
    }


    /**
     * 获取共享模式数据
     * @return
     */
    @GetMapping("/getShareModeData")
    public ResponseServer getShareModeData(){
        List<Classify> shareModeList = classifyDao.queryByIsShare();
        return ResponseServer.success("模式加载成功", shareModeList);
    }


    /**
     * 获取共享产品数据
     * @param pageIndex
     * @return
     */
    @GetMapping("/getShareProductData")
    public ResponseServer getShareProductData(Integer pageIndex){
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", pageIndex);
        params.put("pageSize", 10);
        InitPaging.init(params);
        List<AppProductVO_simplify> productList = productDao.queryProductByClassifyId_simplify(params);
        return ResponseServer.success("产品加载成功", new PageInfo<>(productList));
    }


    /**
     * 获取拼团产品数据
     * @param collageModeId
     * @param pageIndex
     * @return
     */
    @GetMapping("/getCollageProductData")
    public ResponseServer getCollageProductData(Integer collageModeId, Integer pageIndex){
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", pageIndex);
        params.put("pageSize", 10);
        params.put("collageModeId",collageModeId);
        InitPaging.init(params);
        List<AppProductVO_simplify> productList = productDao.queryProductByModeId_simplify(params);
        return ResponseServer.success("加载成功", new PageInfo<>(productList));
    }


    /**
     * 获取产品详情数据
     * @param productId
     * @return
     */
    @GetMapping("/getProductDetailsData")
    public ResponseServer getProductDetailsData(Integer productId){
        Product product = productDao.queryById(productId);
        AppProductVO vo = new AppProductVO();
        BeanUtils.copyProperties(product, vo);
        if(product.getBannerImg().length() > 0){
            String[] strings = product.getBannerImg().split(";");
            List<String> list = new ArrayList();
            for(int i = 0; i < strings.length; i++){
                list.add(strings[i]);
            }
            vo.setBannerImgList(list);
        }
        if(product.getDetailsImg().length() > 0){
            String[] strings1 = product.getDetailsImg().split(";");
            List<String> list1 = new ArrayList();
            for(int y = 0; y < strings1.length; y++){
                list1.add(strings1[y]);
            }
            vo.setDetailsImgList(list1);
        }
        vo.setClassifyName(classifyDao.queryById(product.getClassifyId()).getName());
        vo.setStoreShopName(storeShopDao.queryById(product.getStoreShopId()).getName());
        // 当前团正在进行中的人
        ModeProduct mp = modeProductDao.queryByProductIdByStatus(product.getId());
        if(mp != null){
            List<Orders> ordersList = ordersDao.queryOrdersByCollageProductId(mp.getId());
            if(ordersList.size() > 0){
                List<AppCustomerDTO> acList = new ArrayList();
                for(Orders o : ordersList){
                    Customer c = customerDao.queryById(o.getOrderPersonId());
                    AppCustomerDTO dto = new AppCustomerDTO();
                    dto.setUserId(c.getId());
                    dto.setHeadImg(c.getHeadImg());
                    dto.setLoginName(c.getLoginName());
                    acList.add(dto);
                }
                vo.setUserList(acList);
            }
        }
        List<SkuSVO> skuSVOList = new ArrayList<>();
        SkuSelected skuSelected = skuSelectedDao.querySkuSelectedByProductId(product.getId());
        int index = 0;
        if(skuSelected.getSpecAttribute().contains(",")){
            String[] strings = skuSelected.getSpecAttribute().split(",");
            for(int u = 0 ; u < strings.length; u++){
                String str = strings[u];
                SkuSVO skuSVO = new SkuSVO();
                skuSVO.setId(index);
                String[] strings1 = str.split("\\+");
                String specId = strings1[0];
                skuSVO.setProductId(product.getId());
                skuSVO.setSpecId(Integer.parseInt(specId));
                skuSVO.setSpecName(specAttributeDao.queryById(Integer.parseInt(specId)).getSpecName());
                List<SkuAVO> list = new ArrayList();
                String attIds = strings1[1];
                if(attIds.contains("_")){
                    String[] strings2 = attIds.split("_");
                    for(int p = 0; p < strings2.length; p++){
                        String attId = strings2[p];
                        SkuAVO skuAVO = new SkuAVO();
                        skuAVO.setArrId(Integer.parseInt(attId));
                        skuAVO.setAttName(specAttributeDao.queryById(Integer.parseInt(attId)).getAttributeName());
                        list.add(skuAVO);
                    }
                }else{
                    SkuAVO skuAVO = new SkuAVO();
                    skuAVO.setArrId(Integer.parseInt(attIds));
                    skuAVO.setAttName(specAttributeDao.queryById(Integer.parseInt(attIds)).getAttributeName());
                    list.add(skuAVO);
                }
                skuSVO.setSkuAVOList(list);
                skuSVOList.add(skuSVO);
                index++;
            }
        }else{
            String str = skuSelected.getSpecAttribute();
            SkuSVO skuSVO = new SkuSVO();
            skuSVO.setId(index);
            String[] strings1 = str.split("\\+");
            String specId = strings1[0];
            skuSVO.setProductId(product.getId());
            skuSVO.setSpecId(Integer.parseInt(specId));
            skuSVO.setSpecName(specAttributeDao.queryById(Integer.parseInt(specId)).getSpecName());
            List<SkuAVO> list = new ArrayList();
            String attIds = strings1[1];
            if(attIds.contains("_")){
                String[] strings2 = attIds.split("_");
                for(int p = 0; p < strings2.length; p++){
                    String attId = strings2[p];

                    SkuAVO skuAVO = new SkuAVO();
                    skuAVO.setArrId(Integer.parseInt(attId));
                    skuAVO.setAttName(specAttributeDao.queryById(Integer.parseInt(attId)).getAttributeName());
                    list.add(skuAVO);
                }
            }else{
                SkuAVO skuAVO = new SkuAVO();
                skuAVO.setArrId(Integer.parseInt(attIds));
                skuAVO.setAttName(specAttributeDao.queryById(Integer.parseInt(attIds)).getAttributeName());
                list.add(skuAVO);
            }
            skuSVO.setSkuAVOList(list);
            skuSVOList.add(skuSVO);
        }
        vo.setSkuSVOList(skuSVOList);
        List<SkuRelation> skuRelationList = skuRelationDao.queryByProductId(productId);
        vo.setSkuRVOList(skuRelationList);
        return ResponseServer.success("加载成功", vo);
    }


    /**
     * 立即购买
     * @param parameter
     * @param request
     * @return
     */
    @PostMapping("/buy_now")
    public ResponseServer buy_now(@RequestBody String parameter, HttpServletRequest request){
        System.err.println("立即购买********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        Integer productId = (Integer) jsonObject.get("productId");
        Integer skuId = (Integer) jsonObject.get("skuId");
        Integer buyQuantity = 1; // (Integer) jsonObject.get("buyQuantity");

        Product product = productDao.queryById(productId);
        SkuRelation sku = skuRelationDao.queryById(skuId);

        AppBuyNowCollageVO vo = new AppBuyNowCollageVO();
        vo.setAddressList(addressDao.queryAddressByUserId(customer.getId()));
        vo.setProductId(product.getId());
        vo.setProductName(product.getName());
        vo.setProductImg(product.getImg());
        vo.setUnitPrice(sku.getRetailPrice());
        vo.setBuyQuantity(buyQuantity);
        vo.setFreight(new BigDecimal(0));
        vo.setSkuId(sku.getId());
        vo.setProductType(product.getType());
        String str = "";
        if(sku.getGroupRelation().contains(",")){
            String[] strings = sku.getGroupRelation().split(",");
            for(int k = 0; k < strings.length; k++){
                SpecAttribute spec = specAttributeDao.queryById(Integer.parseInt(strings[k]));
                str += spec.getAttributeName() + ",";
            }
        }else{
            SpecAttribute spec = specAttributeDao.queryById(Integer.parseInt(sku.getGroupRelation()));
            str = spec.getAttributeName() + ",";
        }
        vo.setSpecAttributeName(str.substring(0, str.length()-1));
        vo.setTotalAmount(sku.getRetailPrice().multiply(new BigDecimal(1)));
        vo.setStoreShopFreightList(storeShopFreightDao.queryStoreShopFreightVOById(product.getStoreShopId()));
        if(product.getCouponId() == null){
            vo.setIsNeedUseCoupon("0");
        }else{
            if(product.getClassifyId() != 6 && product.getClassifyId() != 7 && product.getClassifyId() != 8
            && product.getClassifyId() != 9 && product.getClassifyId() != 13 && product.getClassifyId() != 18
            && product.getClassifyId() != 20 && product.getClassifyId() != 21 && product.getClassifyId() != 30
                    && product.getClassifyId() != 40){
                vo.setIsNeedUseCoupon("1");
                UserCoupon uc = userCouponDao.queryByUserIdAndCouponId(customer.getId(), product.getCouponId());
                if(uc == null){
                    List<Coupon> couponList = couponDao.queryAll();
                    if(couponList.size() > 0){
                        for(Coupon cou : couponList){
                            UserCoupon uc1 = userCouponDao.queryByUserIdAndCouponId(customer.getId(), cou.getId());
                            if(uc1 == null){
                                UserCoupon newUc= new UserCoupon();
                                newUc.setCouponId(cou.getId());
                                newUc.setUserId(customer.getId());
                                newUc.setUpdateTime(new Date());
                                newUc.setQuantity(0);
                                userCouponDao.save(newUc);
                            }
                        }
                    }
                }
                Coupon c = couponDao.queryById(product.getCouponId());
                vo.setCouponId(c.getId());
                vo.setCouponName(c.getName());
                UserCoupon uc2 = userCouponDao.queryByUserIdAndCouponId(customer.getId(), product.getCouponId());
                vo.setTotalCouponQuantity(uc2.getQuantity());
                vo.setUseCouponQuantity(product.getUseQuantity());
                vo.setCouponValue(c.getMoney());
            }else{
                vo.setIsNeedUseCoupon("0");
            }
        }
        return ResponseServer.success("加载成功", vo);
    }


    /**
     * 获取订单数据
     * @param request
     * @param status （1=待发货，2=待收货，6=等待开团，7=代售，100=全部）
     * @return
     */
    @GetMapping("/getOrdersData")
    public ResponseServer getOrdersData(HttpServletRequest request, String status, Integer pageIndex){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", pageIndex);
        params.put("pageSize", 10);
        params.put("userId", customer.getId());
        params.put("status", status);
        InitPaging.init(params);
        List<OrdersVO> ordersVOList = ordersDao.queryOrdersVOByStatus_1(params);
        return ResponseServer.success("加载成功", new PageInfo<>(ordersVOList));
    }


    /**
     * 获取订单详情数据
     * @param orderNo
     * @return
     */
    @GetMapping("/getOrderDetailsData")
    public ResponseServer getOrdersDetailsData(String orderNo){
        OrdersVO ordersVO = ordersDao.queryOrdersVOByOrderNo(orderNo);
        return ResponseServer.success("加载成功", ordersVO);
    }


    /**
     * 删除订单 - 假删除
     * @param parameter
     * @return
     */
    @PostMapping("/delete/order")
    public ResponseServer deleteOrders(@RequestBody String parameter){
        System.err.println("删除订单 - 假删除********** parameter ---------- : " + parameter);
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String orderNo = jsonObject.get("orderNo").toString();
        Orders orders = ordersDao.queryByOrderNo(orderNo);
        ordersDao.delete(orders.getId());
        return ResponseServer.success("删除成功");
    }


    /**
     * 确定收货订单
     * @param parameter
     * @return
     */
    @PostMapping("/confirmReceipt/order")
    public ResponseServer confirmReceipt(@RequestBody String parameter){
        System.err.println("确定收货订单********** parameter ---------- : " + parameter);
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String orderNo = jsonObject.get("orderNo").toString();
        Orders orders = ordersDao.queryByOrderNo(orderNo);
        if("1".equals(orders.getPayStatus()) && "2".equals(orders.getDealStatus())){
            orders.setDealStatus("3");
            orders.setConfirmTime(new Date());
            ordersDao.update(orders);
            return ResponseServer.success("确认收货成功");
        }else{
            return ResponseServer.error("确认收货失败，订单状态不正确");
        }
    }


    /**
     * 获取地址数据
     * @param request
     * @return
     */
    @GetMapping("/getAddressData")
    public ResponseServer getAddressData(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        return ResponseServer.success("加载成功", addressDao.queryAllAddressVOByUserId(customer.getId()));
    }


    /**
     * 添加或修改地址
     * @param request
     * @param parameter
     * @return
     */
    @PostMapping("/address/saveOrUpdate")
    public ResponseServer saveAddress(@RequestBody String parameter, HttpServletRequest request){
        System.err.println("添加或修改地址********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String province = jsonObject.get("province").toString();
        String city = jsonObject.get("city").toString();
        String county = jsonObject.get("county").toString();
        String address = jsonObject.get("address").toString();
        String consigneeName = jsonObject.get("consigneeName").toString();
        String consigneePhone = jsonObject.get("consigneePhone").toString();
        String isDefault = jsonObject.get("isDefault").toString();
        if("1".equals(isDefault)) {
            Address updateIdentity = addressDao.findAddressByIsDefault(customer.getId(), isDefault);
            if (updateIdentity != null) {
                updateIdentity.setIsDefault("0");
                addressDao.update(updateIdentity);
            }
        }
        if(jsonObject.get("addressId") == null){
            // 新增
            Address add = new Address();
            add.setUserId(customer.getId());
            add.setUpdateTime(new Date());
            add.setIsDefault(isDefault);
            add.setFreight("0");
            add.setProvince(province);
            add.setCity(city);
            add.setCounty(county);
            add.setAddress(address);
            add.setFullAddress(province+city+county+address);
            add.setConsigneeName(consigneeName);
            add.setConsigneePhone(consigneePhone);
            addressDao.save(add);
            return ResponseServer.success("地址添加成功", add);
        }else{
            // 修改
            Integer addressId = (Integer) jsonObject.get("addressId");
            Address up = addressDao.queryById(addressId);
            up.setIsDefault(isDefault);
            up.setProvince(province);
            up.setCity(city);
            up.setCounty(county);
            up.setAddress(address);
            up.setFullAddress(province+city+county+address);
            up.setConsigneeName(consigneeName);
            up.setConsigneePhone(consigneePhone);
            addressDao.update(up);
            return ResponseServer.success("地址修改成功", up);
        }

    }


    /**
     * 根据ID删除地址
     * @param parameter
     * @return
     */
    @PostMapping("/address/delete")
    public ResponseServer deleteAddress(@RequestBody String parameter){
        System.err.println("根据ID删除地址********** parameter ---------- : " + parameter);
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        Integer addressId = (Integer) jsonObject.get("addressId");
        addressDao.delete(addressId);
        return ResponseServer.success("删除成功");
    }


    /**
     * 获取账单明细数据
     * @param request
     * @param type
     * @param pageIndex
     * @return
     */
    @GetMapping("/getAccountRecordData")
    public ResponseServer getAccountRecordData(HttpServletRequest request, String type, Integer pageIndex){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        Map<String, Object> params = new HashMap<>();
        params.put("userId", customer.getId());
        params.put("type", type);
        params.put("pageNum", pageIndex);
        params.put("pageSize", 10);
        InitPaging.init(params);
        List<AccountRecord> accountRecordList = accountRecordDao.queryByUserId(params);
        return ResponseServer.success("余额记录加载成功", new PageInfo<>(accountRecordList));
    }


    /**
     * 获取红包数据
     * @param request
     * @return
     */
    @GetMapping("/getRedPackData")
    public ResponseServer getRedPackData(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        //Map map = new HashMap();
        List<RedPackSpecDTO> dtoList = new ArrayList<>();
        List<OpenRedPack> openRedPackList = openRedPackDao.queryByStatus(customer.getId());
        if(openRedPackList.size() > 0){
            for(OpenRedPack orp : openRedPackList){
                RedPackSpecDTO dto = new RedPackSpecDTO();
                dto.setRedPackId(orp.getId());
                if(orp.getPrizeMoney().doubleValue() >= 369){
                    RedPackSpec rps = redPackSpecDao.queryByType("0"); // 369红包
                    dto.setImg(rps.getImg());
                    dto.setType("0");
                }else{
                    if("1".equals(orp.getType())){
                        RedPackSpec rps = redPackSpecDao.queryByType("2"); // 周红包
                        dto.setImg(rps.getImg());
                        dto.setType("1");
                    }else{
                        RedPackSpec rps = redPackSpecDao.queryByType("1"); // 非369红包
                        dto.setImg(rps.getImg());
                        dto.setType("1");
                    }
                }
                dtoList.add(dto);
            }
        }
        //map.put("shareSuccess", openRedPackList);
        List<FailRedPack> failRedPackList = failRedPackDao.queryByUserId(customer.getId());
        if(failRedPackList.size() > 0){
            for(FailRedPack frp : failRedPackList){
                RedPackSpecDTO dto = new RedPackSpecDTO();
                dto.setRedPackId(frp.getId());
                if("佣金".equals(frp.getSign())){
                    RedPackSpec rps = redPackSpecDao.queryByType("4"); // 佣金
                    dto.setImg(rps.getImg());
                    dto.setType("4");
                }else{
                    RedPackSpec rps = redPackSpecDao.queryByType("3"); // 拼团红包
                    dto.setImg(rps.getImg());
                    dto.setType("3");
                }
                dtoList.add(dto);
            }
        }
        //map.put("collageFail", failRedPackList);

        return ResponseServer.success("加载成功", dtoList);
    }


    /**
     * 点击开红包
     * @param request
     * @param parameter
     * @return
     */
    @PostMapping("/clickOpenRedPack")
    public ResponseServer clickOpenRedPack(@RequestBody String parameter, HttpServletRequest request){
        System.err.println("点击开红包********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        Integer redPackId = (Integer) jsonObject.get("redPackId");
        String type = jsonObject.get("type").toString();
        // 选择的红包类型（0=共享红包369，1=共享红包非369，2=周红包，3=拼团红包，4=佣金红包）
        BigDecimal bd = new BigDecimal(0);
        if("0".equals(type) || "1".equals(type)){
            // 共享
            OpenRedPack openRedPack = openRedPackDao.queryById(redPackId);
            if(openRedPack != null){
                if("2".equals(openRedPack.getStatus())){
                    Account account = accountDao.queryAccountByUserId(openRedPack.getUserId());
                    BigDecimal frozenIn = openRedPack.getPrizeMoney().multiply(BigDecimal.valueOf(0.3));
                    BigDecimal shareBalance = openRedPack.getPrizeMoney().multiply(BigDecimal.valueOf(0.7));
                    account.setShareBalance(account.getShareBalance().add(shareBalance));
                    account.setFrozenIn(account.getFrozenIn().add(frozenIn));
                    accountDao.update(account);
                    AccountRecord ar1 = new AccountRecord();
                    ar1.setIntoUserId(openRedPack.getUserId());
                    ar1.setUpdateTime(new Date());
                    ar1.setType("10");
                    ar1.setAmount(shareBalance);
                    ar1.setNowAmount(account.getShareBalance());
                    ar1.setSign("1");
                    ar1.setInfo("您开共享红包! 获得共享余额：" + shareBalance);
                    accountRecordDao.save(ar1);
                    AccountRecord ar2 = new AccountRecord();
                    ar2.setIntoUserId(openRedPack.getUserId());
                    ar2.setUpdateTime(new Date());
                    ar2.setType("10");
                    ar2.setAmount(frozenIn);
                    ar2.setNowAmount(account.getFrozenIn());
                    ar2.setSign("1");
                    ar2.setInfo("您开共享红包! 获得共享余额：(冻结)" + frozenIn);
                    accountRecordDao.save(ar2);
                    openRedPack.setStatus("1");
                    openRedPackDao.update(openRedPack);
                    bd = openRedPack.getPrizeMoney();
                    if(account.getFrozenIn().doubleValue() >= 369){
                        // 进入复消
                        OrdersShareRepeat osr = new OrdersShareRepeat();
                        osr.setUserId(account.getUserId());
                        osr.setUpdateTime(new Date());
                        osr.setStatus("0");
                        ordersShareRepeatDao.save(osr);
                    }
                }
            }
        }else{
            // 拼团
            if("3".equals(type)){
                FailRedPack failRedPack = failRedPackDao.queryById(redPackId);
                if(failRedPack != null){
                    if("2".equals(failRedPack.getStatus())){
                        failRedPack.setStatus("1");
                        failRedPackDao.update(failRedPack);
                        Account account = accountDao.queryAccountByUserId(failRedPack.getUserId());
                        bd = failRedPack.getPrizeMoney();
                        if("0".equals(failRedPack.getMode())){
                            account.setVoucherBalance(account.getVoucherBalance().add(failRedPack.getPrizeMoney()));
                            accountDao.update(account);
                            AccountRecord ar1 = new AccountRecord();
                            ar1.setIntoUserId(failRedPack.getUserId());
                            ar1.setUpdateTime(new Date());
                            ar1.setType("0");
                            ar1.setAmount(failRedPack.getPrizeMoney());
                            ar1.setNowAmount(account.getVoucherBalance());
                            ar1.setSign("1");
                            ar1.setOrderNo(failRedPack.getOrderNo());
                            ar1.setInfo(customer.getAgentCode() + "开拼团红包! 获得余额：" + failRedPack.getPrizeMoney());
                            accountRecordDao.save(ar1);
                            // 处理参与拼团后的利润分配
                            ordersCopyService.handleCollageProfit(failRedPack.getUserId(), failRedPack.getPrizeMoney(), failRedPack.getOrderNo());
                        }else{
                            account.setVoucherBalance(account.getVoucherBalance().add(failRedPack.getPrizeMoney()));
                            accountDao.update(account);
                            AccountRecord ar1 = new AccountRecord();
                            ar1.setIntoUserId(failRedPack.getUserId());
                            ar1.setUpdateTime(new Date());
                            ar1.setType("0");
                            ar1.setAmount(failRedPack.getPrizeMoney());
                            ar1.setNowAmount(account.getVoucherBalance());
                            ar1.setSign("1");
                            ar1.setOrderNo(failRedPack.getOrderNo());
                            ar1.setInfo("您开拼团红包! 获得余额：" + failRedPack.getPrizeMoney());
                            accountRecordDao.save(ar1);
                            // 新团，模式ID为30的
                            // ordersService.handleCollageProfit30(failRedPack.getUserId(), failRedPack.getPrizeMoney(), failRedPack.getOrderNo());
                        }
                    }
                }
            }else{
                // 周红包
                if("2".equals(type)){
                    OpenRedPack openRedPack = openRedPackDao.queryById(redPackId);
                    if(openRedPack != null){
                        if("2".equals(openRedPack.getStatus())){
                            Account account = accountDao.queryAccountByUserId(openRedPack.getUserId());
                            BigDecimal frozenIn = openRedPack.getPrizeMoney().multiply(BigDecimal.valueOf(0.3));
                            BigDecimal shareBalance = openRedPack.getPrizeMoney().multiply(BigDecimal.valueOf(0.7));
                            account.setShareBalance(account.getShareBalance().add(shareBalance));
                            account.setFrozenIn(account.getFrozenIn().add(frozenIn));
                            accountDao.update(account);
                            AccountRecord ar1 = new AccountRecord();
                            ar1.setIntoUserId(openRedPack.getUserId());
                            ar1.setUpdateTime(new Date());
                            ar1.setType("10");
                            ar1.setAmount(shareBalance);
                            ar1.setNowAmount(account.getShareBalance());
                            ar1.setSign("1");
                            ar1.setInfo("您开共享周红包! 获得共享余额：" + shareBalance);
                            accountRecordDao.save(ar1);
                            AccountRecord ar2 = new AccountRecord();
                            ar2.setIntoUserId(openRedPack.getUserId());
                            ar2.setUpdateTime(new Date());
                            ar2.setType("10");
                            ar2.setAmount(frozenIn);
                            ar2.setNowAmount(account.getFrozenIn());
                            ar2.setSign("1");
                            ar2.setInfo("您开共享周红包! 获得共享余额：(冻结)" + frozenIn);
                            accountRecordDao.save(ar2);
                            openRedPack.setStatus("1");
                            openRedPackDao.update(openRedPack);
                            bd = openRedPack.getPrizeMoney();
                        }
                    }
                }else{
                    // 佣金红包
                    if("4".equals(type)) {
                        FailRedPack failRedPack = failRedPackDao.queryById(redPackId);
                        if (failRedPack != null) {
                            if ("2".equals(failRedPack.getStatus())) {
                                failRedPack.setStatus("1");
                                failRedPackDao.update(failRedPack);
                                Account account = accountDao.queryAccountByUserId(failRedPack.getUserId());
                                bd = failRedPack.getPrizeMoney();
                                account.setCollageBalance(account.getCollageBalance().add(failRedPack.getPrizeMoney()));
                                accountDao.update(account);
                                AccountRecord ar1 = new AccountRecord();
                                ar1.setIntoUserId(failRedPack.getUserId());
                                ar1.setUpdateTime(new Date());
                                ar1.setType("8");
                                ar1.setAmount(failRedPack.getPrizeMoney());
                                ar1.setNowAmount(account.getShareBalance());
                                ar1.setSign("1");
                                ar1.setOrderNo(failRedPack.getOrderNo());
                                ar1.setInfo("您开佣金红包! 获得拼团余额：" + failRedPack.getPrizeMoney());
                                accountRecordDao.save(ar1);
                            }
                        }
                    }
                }
            }
        }
        return ResponseServer.success("开红包成功", bd.toString());
    }


    /**
     * 购买共享产品，订单支付
     * @param request
     * @param parameter  [payType] （0=微信支付，1=余额支付，2=积分支付，3=拼团余额支付，4=app微信支付，5=app支付宝支付，6=共享余额支付，10=威富通微信支付，11=威富通支付宝支付）
     * @return
     * @throws Exception
     */
    @PostMapping("/order/pay/share")
    public ResponseServer orderPayShare(@RequestBody String parameter, HttpServletRequest request) throws Exception {
        System.err.println("购买共享产品，订单支付********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
//        if(customer != null){
//            return ResponseServer.error(".敬请期待!");
//        }
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        Integer productId = (Integer) jsonObject.get("productId");
        Integer skuId = (Integer) jsonObject.get("skuId");
        Integer addressId = (Integer) jsonObject.get("addressId");
        String oldFreight = jsonObject.get("freight").toString();
        Integer useCouponQuantity = (Integer) jsonObject.get("useCouponQuantity");
        Integer couponId = (Integer) jsonObject.get("couponId");
        String payType = jsonObject.get("payType").toString();
        Integer buyQuantity = 1;//(Integer) jsonObject.get("buyQuantity");

        SkuRelation sku = skuRelationDao.queryById(skuId);
        if(sku == null){
            return ResponseServer.error("支付失败,产品SKU错误");
        }

        Address address = addressDao.queryById(addressId);
        if(address == null){
            return ResponseServer.error("支付失败,收货地址错误");
        }

        Product product = productDao.queryById(productId);
        if(product == null){
            return ResponseServer.error("支付失败,产品信息错误");
        }

        //BigDecimal freight1 = new BigDecimal(0);
        List<StoreShopFreightVO> ssfList = storeShopFreightDao.queryStoreShopFreightVOById(product.getStoreShopId());
        if(ssfList.size() > 0){
            for(StoreShopFreightVO ssf : ssfList){
                if(ssf.getChinaAreasName().equals(address.getProvince())){
                    if("0".equals(ssf.getStatus())){
                        return ResponseServer.error("该区域商家暂不配送");
                    }
                    //else{
                        //freight1 = ssf.getPrice();
                        //break;
                    //}
                }
            }
        }
        //if(freight1.doubleValue() != freight.doubleValue()){
            //return ResponseServer.error("该区域配送的运费不正确");
        //}

        Account account = accountDao.queryAccountByUserId(customer.getId());
        if(account == null){
            return ResponseServer.error("支付失败,账户信息错误,请联系管理员");
        }

        BigDecimal freight = BigDecimal.valueOf(Long.parseLong(oldFreight));
        if (freight == null) {
            freight = BigDecimal.valueOf(0.0);
        }

        Date date = new Date();

        BigDecimal totalPrice; // 支付的钱
        // 商户订单号
        String outTradeNo = "BEE" + Units.getOrderIdByTime().replaceAll("-", "");
        // 交易号
        String tradeNo = "JYH" + Units.getOrderIdByTime().replaceAll("-", "");

        BigDecimal couponPrice;
        if(couponId == null && useCouponQuantity == null){
            couponPrice = new BigDecimal(0);
        }else{
            couponPrice = BigDecimal.valueOf(99).multiply(BigDecimal.valueOf(useCouponQuantity));
        }

        BigDecimal totalAmount = sku.getRetailPrice().multiply(BigDecimal.valueOf(buyQuantity)).add(freight); // 总金额
        totalPrice = totalAmount.subtract(couponPrice);

        // 创建订单
        Orders orders = new Orders();
        orders.setOrderNo(outTradeNo);
        orders.setTradeNo(tradeNo);
        orders.setTotalAmount(totalAmount); // 总金额
        orders.setPayAmount(totalPrice);  // 实际支付金额
        orders.setCouponAmount(couponPrice);  // 优惠券抵扣金额
        orders.setFreight(freight); // 运费金额
        orders.setPayStatus("0");
        orders.setPayType(payType);
        orders.setDealStatus("0");
        orders.setRefundStatus("0");
        orders.setPrepayId("");
        orders.setOrderPersonId(customer.getId());
        orders.setConsigneeName(address.getConsigneeName());
        orders.setConsigneePhone(address.getConsigneePhone());
        orders.setAddress(address.getFullAddress());
        orders.setUpdateTime(date);
        orders.setIsDel("1");
        orders.setIsRun("0");
        orders.setRemarks("无");
        orders.setProductId(product.getId());
        ordersDao.save(orders);
        // 订单详情
        OrderDetails od = new OrderDetails();
        od.setOrderNo(orders.getOrderNo());
        od.setTradeNo(orders.getTradeNo());
        od.setStoreShopId(product.getStoreShopId());
        od.setStoreShopName(storeShopDao.queryById(product.getStoreShopId()).getName());
        od.setClassifyId(product.getClassifyId());
        od.setClassifyName(classifyDao.queryById(product.getClassifyId()).getName());
        od.setProductId(product.getId());
        od.setProductType(product.getType());
        od.setProductImg(product.getImg());
        od.setProductName(product.getName());
        od.setProductCode(product.getProductCode());
        od.setCostPrice(sku.getCostPrice());
        od.setRelationId(sku.getId());
        if(sku.getGroupRelation().contains(",")){
            String[] strings = sku.getGroupRelation().split(",");
            String str = "";
            for(int u = 0; u < strings.length; u++){
                str += specAttributeDao.queryById(Integer.parseInt(strings[u])).getAttributeName() + "，";
            }
            od.setSpecAttributeName(str.substring(0, str.length()-1));
        }else{
            od.setSpecAttributeName(specAttributeDao.queryById(Integer.parseInt(sku.getGroupRelation())).getAttributeName());
        }
        od.setQuantity(buyQuantity);
        od.setUnitPrice(sku.getRetailPrice());
        od.setUpdateTime(date);
        orderDetailsDao.save(od);

        if("6".equals(payType)){ // 共享余额支付

            if(account.getShareBalance().doubleValue() < totalPrice.doubleValue()){
                return ResponseServer.error("支付失败, 共享余额不足");
            }
            account.setShareBalance(account.getShareBalance().subtract(totalPrice));
            //int give = (int)Math.round(totalPrice.doubleValue());
            //account.setIntegral(account.getIntegral() + give);
            accountDao.update(account);

            AccountRecord ar = new AccountRecord();
            ar.setOutUserId(customer.getId());
            ar.setUpdateTime(date);
            ar.setType("10");
            ar.setAmount(totalPrice);
            ar.setNowAmount(account.getShareBalance());
            ar.setSign("2");
            ar.setOrderNo(outTradeNo);
            ar.setInfo(customer.getAgentCode() + "购买成功! 消费共享余额：" + totalPrice);
            accountRecordDao.save(ar);
//            AccountRecord ar1 = new AccountRecord();
//            ar1.setIntoUserId(customer.getId());
//            ar1.setUpdateTime(date);
//            ar1.setType("1");
//            ar1.setAmount(BigDecimal.valueOf(give));
//            ar1.setNowAmount(BigDecimal.valueOf(account.getIntegral()));
//            ar1.setSign("1");
//            ar1.setOrderNo(outTradeNo);
//            ar1.setInfo(customer.getAgentCode() + "购买成功! 您获得积分：" + give);
//            accountRecordDao.save(ar1);

            // 优惠券扣除
            int uu = (int) (couponPrice.doubleValue() / 99);
            if(uu > 0){
                UserCoupon userCoupon = userCouponDao.queryByUserIdAndCouponId(customer.getId(), couponId);
                userCoupon.setQuantity(userCoupon.getQuantity() - uu);
                userCouponDao.update(userCoupon);
                AccountRecord accRe = new AccountRecord();
                accRe.setOutUserId(customer.getId());
                accRe.setUpdateTime(date);
                accRe.setType("6");
                accRe.setAmount(BigDecimal.valueOf(uu));
                accRe.setNowAmount(BigDecimal.valueOf(userCoupon.getQuantity()));
                accRe.setSign("2");
                accRe.setOrderNo(outTradeNo);
                accRe.setInfo(customer.getAgentCode() + "购买成功! 消费共享券：" + uu);
                accountRecordDao.save(accRe);
            }

            // 修改订单
            orders.setPayStatus("1");
            orders.setDealStatus("1");
            orders.setIsDel("0");
            ordersDao.update(orders);

            // 添加销量
            product.setSales(product.getSales() + 1);
            productDao.update(product);

            // 周奖金池，总奖金池，红包券，擂台奖池，爱心奖池
            sharingMode(orders);

            // 添加业绩
            addAchievementAndOrderQuantity(orders);

            // 升级
            shareUpgrade(customer, orders.getTotalAmount());

            // 分润
            shareSplitProfit(customer, orders);

            return ResponseServer.success("共享余额支付成功!!!");

        }else if("4".equals(payType)){
            Map<String, Object> wxPay = new HashMap<>();
            wxPay.put("totalPrice", String.valueOf(totalPrice));
            wxPay.put("outTradeNo", outTradeNo);
            wxPay.put("notifyUrl", Constants.wxPay_notify_url_share);
            wxPay.put("wxPayAppId", Constants.app_appid);
            wxPay.put("tradeType", Constants.app_trade_type);
            String paramBuffer = CreateUnifiedOrder.UnifiedOrder(request, wxPay);
            Map<String, String> map = Units.doXMLParse(HttpRequestUtil.sendPost(Constants.pay_url, new String(paramBuffer.getBytes(), "UTF-8")));
            if (map != null) {
                String prepayId = map.get("prepay_id");
                net.sf.json.JSONObject resultJson = CreateUnifiedOrder.generateSignature(prepayId, Constants.app_appid);
                resultJson.put("prepayId", prepayId);
                resultJson.put("package", "Sign=WXPay");
                return ResponseServer.success(resultJson);
            }else{
                return ResponseServer.error("调用微信支付失败");
            }

        }else if("5".equals(payType)){
            Map<String, Object> aliPay = new HashMap<>();
            aliPay.put("totalFee", String.valueOf(totalPrice));
            aliPay.put("outTradeNo", outTradeNo);
            aliPay.put("body", "优选商品");
            ResponseServer responseServer = AliPayUtil.aliPaymentShare(aliPay);
            if (responseServer.getData() != null) {
                Map<String, Object> map = new HashMap<>();
                map.put("form", responseServer.getData());
                return ResponseServer.success(map);
            }else{
                return ResponseServer.error("调用支付宝支付失败");
            }

        }else{
            return ResponseServer.error("支付失败, 支付方式错误");
        }

    }


    /**
     * 购买共享产品，订单支付 - 微信回调
     * @param request
     * @param response
     */
    @PostMapping("/wxPay_notify_url_share")
    public void wxPaySharePayCallback(HttpServletRequest request, HttpServletResponse response) {
        String inputLine;
        String notifyXml = "";
        try {
            while ((inputLine = request.getReader().readLine()) != null) {
                notifyXml += inputLine;
            }
            // 关闭流
            request.getReader().close();
            // 解析成map
            Map<String, String> map = Units.doXMLParse(notifyXml);
            // 判断支付是否成功
            if ("SUCCESS".equals(map.get("result_code"))) {
                System.err.println("微信回调返回是否支付成功：是");
                //获得返回的商户订单号
                String outTradeNo = map.get("out_trade_no");
                System.err.println("微信回调返回商户订单号：" + outTradeNo);

                Orders transmitOrders = ordersDao.queryByOrderNo(outTradeNo);
                if("0".equals(transmitOrders.getDealStatus())) {
                    transmitOrders.setPayStatus("1");
                    transmitOrders.setDealStatus("1");
                    transmitOrders.setIsDel("0");
                    ordersDao.update(transmitOrders);

                    Product product = productDao.queryById(transmitOrders.getProductId());
                    product.setSales(product.getSales() + 1); // 添加销量
                    productDao.update(product);

                    Customer customer = customerDao.queryById(transmitOrders.getOrderPersonId());

                    //Account acc = accountDao.queryAccountByUserId(customer.getId());
                    //int give = (int)Math.round(transmitOrders.getPayAmount().doubleValue());
                    //acc.setIntegral(acc.getIntegral() + give);
                    //accountDao.update(acc);

//                AccountRecord ar = new AccountRecord();
//                ar.setIntoUserId(customer.getId());
//                ar.setUpdateTime(new Date());
//                ar.setType("1");
//                ar.setAmount(BigDecimal.valueOf(give));
//                ar.setNowAmount(BigDecimal.valueOf(acc.getIntegral()));
//                ar.setSign("1");
//                ar.setOrderNo(outTradeNo);
//                ar.setInfo(customer.getAgentCode() + "购买成功! 您获得积分：" + give);
//                accountRecordDao.save(ar);

                    // 周奖金池，总奖金池，红包券，擂台奖池，爱心奖池
                    sharingMode(transmitOrders);

                    // 添加业绩
                    addAchievementAndOrderQuantity(transmitOrders);

                    // 升级
                    shareUpgrade(customer, transmitOrders.getTotalAmount());

                    // 分润
                    shareSplitProfit(customer, transmitOrders);
                }

                System.err.println("微信支付回调处理完成");
                //封装 返回值
                StringBuffer buffer = new StringBuffer();
                buffer.append("<xml>");
                buffer.append("<return_code>SUCCESS</return_code>");
                buffer.append("<return_msg>OK</return_msg>");
                buffer.append("</xml>");
                //给微信服务器返回 成功标示 否则会一直询问 咱们服务器 是否回调成功
                PrintWriter writer = response.getWriter();
                //返回
                writer.print(buffer.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 购买共享产品，订单支付 - 支付宝回调
     * @param request
     * @param response
     * @param modelMap
     * @throws Exception
     */
    @PostMapping("/aliPay_notify_url_share")
    public void aliPaySharePayCallback(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
        System.err.println("支付宝支付回调---------- modelMap ---------->>>> " + modelMap);
        // 获取支付宝POST过来反馈信息
        String result = "false";
        try {
            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
                String name = iter.next();
                String[] values = requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i]
                            : valueStr + values[i] + ",";
                }
                // 乱码解决
                valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
                params.put(name, valueStr);
            }
            System.err.println("支付宝回调信息：" + params);
            // 调用SDK验证签名
            boolean signVerified = AlipaySignature.rsaCheckV1(params, Constants.aliPay_public_key, Constants.aliPay_charset, Constants.aliPay_sign_type);

    	    /* 实际验证过程建议商户务必添加以下校验：
    		1、需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号，
    		2、判断total_amount是否确实为该订单的实际金额（即商户订单创建时的金额），
    		3、校验通知中的seller_id（或者seller_email) 是否为out_trade_no这笔单据的对应的操作方（有的时候，一个商户可能有多个seller_id/seller_email）
    		4、验证app_id是否为该商户本身。*/

            System.err.println("验证签名:" + signVerified);
            result = "success";
            // 商户订单号
            String outTradeNo = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
            System.err.println("订单号：" + outTradeNo);
            String total_amount = request.getParameter("total_amount"); // 获取总金额
            System.err.println("订单金额：" + total_amount);
            // 支付宝交易号
            String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
            System.err.println("支付宝交易号：" + trade_no);
            //交易状态
            String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");
            System.err.println("交易状态：" + trade_status);
            if (trade_status.equals("TRADE_SUCCESS")) {
                System.err.println("TRADE_SUCCESS");
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序
                //注意：
                //退款日期超过可退款期限后（如三个月可退款），支付宝系统发送该交易状态通知
                System.err.println("支付宝回调返回是否支付成功：是");

                Orders transmitOrders = ordersDao.queryByOrderNo(outTradeNo);
                if("0".equals(transmitOrders.getDealStatus())) {
                    transmitOrders.setPayStatus("1");
                    transmitOrders.setDealStatus("1");
                    transmitOrders.setIsDel("0");
                    ordersDao.update(transmitOrders);

                    Product product = productDao.queryById(transmitOrders.getProductId());
                    product.setSales(product.getSales() + 1); // 添加销量
                    productDao.update(product);

                    Customer customer = customerDao.queryById(transmitOrders.getOrderPersonId());

                    // 周奖金池，总奖金池，红包券，擂台奖池，爱心奖池
                    sharingMode(transmitOrders);

                    // 添加业绩
                    addAchievementAndOrderQuantity(transmitOrders);

                    // 升级
                    shareUpgrade(customer, transmitOrders.getTotalAmount());

                    // 分润
                    shareSplitProfit(customer, transmitOrders);
                }

                System.err.println("支付宝支付回调处理完成");
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=" + Constants.aliPay_charset);
        response.setCharacterEncoding(Constants.aliPay_charset);
        response.getWriter().write(result); // 直接将完整的表单html输出到页面
        response.getWriter().flush();
        response.getWriter().close();
        return;
    }


    /**
     * 共享模式 处理用户购买后的奖金池，红包券，分润
     * @param orders
     */
    public void sharingMode(Orders orders){
        try {
            // 下单人
            Customer customer = customerDao.queryById(orders.getOrderPersonId());

            // 上级信息
            Customer parentCustomer = null;
            Account parentAccount = null;
            if(customer.getParentId() != null && customer.getParentId() != 1198){
                parentCustomer = customerDao.queryById(customer.getParentId());
                parentAccount = accountDao.queryAccountByUserId(parentCustomer.getId());
            }

            // 给直推累计擂台金额
            parentCustomer.setTodayArean(parentCustomer.getTodayArean().add(orders.getTotalAmount()));
            customerDao.update(parentCustomer);

            // 系统当前时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date toDate = new Date();
            Calendar now = Calendar.getInstance();
            now.setTime(toDate);
            String date = sdf.format(now.getTime());
            now.add(Calendar.DAY_OF_MONTH, -1);
            String date1 = sdf.format(now.getTime());
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 获取周奖金池
            BigDecimal weeklyAmount = orders.getPayAmount().multiply(BigDecimal.valueOf(0.4)); // 40%
            PrizePoolWeekly ppw = prizePoolWeeklyDao.queryByBeginTimeAndEndTime(format1.format(toDate));
            if(ppw == null){
                PrizePoolWeekly newW = new PrizePoolWeekly();
                newW.setBeginTime(toDate);
                newW.setPayMoney(weeklyAmount);

                int num = 6; // 加的天数
                String endTime = format1.format(toDate);
                Date currDate = format1.parse(endTime);
                Calendar ca = Calendar.getInstance();
                ca.setTime(currDate);
                ca.add(Calendar.DATE, num);
                currDate = ca.getTime();
                String nice = sdf.format(currDate) + " 23:59:59";
                newW.setEndTime(format1.parse(nice));
                newW.setSplitMoney(new BigDecimal(0));
                prizePoolWeeklyDao.save(newW);
            }else{
                ppw.setPayMoney(ppw.getPayMoney().add(weeklyAmount));
                prizePoolWeeklyDao.update(ppw);
            }

            // 获取今日奖金池
            PrizePool prizePool = prizePoolDao.queryByToday(date);
            if(prizePool == null){
                prizePool = prizePoolDao.queryByToday(date1); // TODO 每天凌晨0点-2点的共享订单，统计到昨天计算，2点后的统计到今日计算
            }
            prizePool.setTotalMoney(prizePool.getTotalMoney().add(orders.getTotalAmount())); // 加奖池 -产品金额
            prizePool.setPayMoney(prizePool.getPayMoney().add(orders.getPayAmount())); // 支付金额

            // 一共的金额
            BigDecimal sumAmount = orders.getTotalAmount().add(customer.getOwnTotalBuy3());
            customer.setOwnTotalBuy3(sumAmount);
            customerDao.update(customer);

            // 分红包券
            if(orders.getTotalAmount().doubleValue() >= 369 && orders.getTotalAmount().doubleValue() < 3690){
                prizePool.setRedPackQuantity(prizePool.getRedPackQuantity() + 1); // 加奖金池红包券
                parentAccount.setRedPacket(parentAccount.getRedPacket() + 1);// 加上级个人红包券
            }else if(orders.getTotalAmount().doubleValue() >= 3690 && orders.getTotalAmount().doubleValue() < 36900){
                prizePool.setRedPackQuantity(prizePool.getRedPackQuantity() + 10); // 加奖金池红包券
                parentAccount.setRedPacket(parentAccount.getRedPacket() + 10);// 加上级个人红包券
            }else if(orders.getTotalAmount().doubleValue() >= 36900){
                prizePool.setRedPackQuantity(prizePool.getRedPackQuantity() + 100); // 加奖金池红包券
                parentAccount.setRedPacket(parentAccount.getRedPacket() + 100);// 加上级个人红包券
            }
            accountDao.update(parentAccount);
            prizePoolDao.update(prizePool);

            // 爱心奖金池 1%
            PrizePoolLove love = prizePoolLoveDao.queryByToday(date); // 每日
            if(love == null){
                // 创建今日
                PrizePoolLove newLove = new PrizePoolLove();
                newLove.setUpdateTime(new Date());
                newLove.setSplitMoney(new BigDecimal(0));
                newLove.setPayMoney(orders.getTotalAmount().multiply(BigDecimal.valueOf(0.01)));
                prizePoolLoveDao.save(newLove);
            }else{
                // 今日继续累加
                love.setPayMoney(love.getPayMoney().add(orders.getTotalAmount().multiply(BigDecimal.valueOf(0.01))));
                prizePoolLoveDao.update(love);
            }

            // 擂台奖金池 4%
            PrizePoolArean arean = prizePoolAreanDao.queryByToday(date); // 每日
            if(arean == null){
                // 创建今日
                PrizePoolArean newArean = new PrizePoolArean();
                newArean.setUpdateTime(new Date());
                newArean.setSplitMoney(new BigDecimal(0));
                newArean.setPayMoney(orders.getTotalAmount().multiply(BigDecimal.valueOf(0.04)));
                prizePoolAreanDao.save(newArean);
            }else{
                // 今日继续累加
                arean.setPayMoney(arean.getPayMoney().add(orders.getTotalAmount().multiply(BigDecimal.valueOf(0.04))));
                prizePoolAreanDao.update(arean);
            }

        }catch (Exception e){
            e.printStackTrace();

        }finally {

        }
    }


    /**
     * 共享模式 处理用户购买后的升级
     * @param customer
     * @param totalAmount
     */
    public void shareUpgrade(Customer customer, BigDecimal totalAmount){
        if(totalAmount.doubleValue() >= 369 && totalAmount.doubleValue() < 3690){
            if(customer.getLevelCopy3Id() < 2){
                customer.setLevelCopy3Id(2);
                if("0".equals(customer.getIsActiveCopy3())){
                    customer.setIsActiveCopy3("1"); // 激活共享另一套等级
                }
                customerDao.update(customer);
            }
        }else if(totalAmount.doubleValue() >= 3690 && totalAmount.doubleValue() < 36900){
            if(customer.getLevelCopy3Id() < 3){
                customer.setLevelCopy3Id(3);
                if("0".equals(customer.getIsActiveCopy3())){
                    customer.setIsActiveCopy3("1"); // 激活共享另一套等级
                }
                customerDao.update(customer);
            }
        }else if(totalAmount.doubleValue() >= 36900){
            if(customer.getLevelCopy3Id() < 4){
                customer.setLevelCopy3Id(4);
                if("0".equals(customer.getIsActiveCopy3())){
                    customer.setIsActiveCopy3("1"); // 激活共享另一套等级
                }
                customerDao.update(customer);
            }
        }

        // 处理共享另一套等级
        if(customer.getParentId() != null && customer.getParentId() != 1198 && customer.getLevelCopy4Id() != 8){
            Customer cu1 = customerDao.queryById(customer.getParentId());
            if(cu1 != null){
                if(cu1.getLevelCopy4Id() == 1){ // v0
                    // 判断是否升V1
                    // 查询下级是否有两条线满足50单
                    int rowSize1 = customerDao.queryByOrderQuantity50(cu1.getId());
                    if(rowSize1 >= 2){
                        cu1.setLevelCopy4Id(2);
                        customerDao.update(cu1);

                        if(cu1.getParentId() != null && cu1.getParentId() != 1198 && cu1.getLevelCopy4Id() != 8){
                            Customer cu2 = customerDao.queryById(cu1.getParentId());
                            if(cu2 != null){
                                // 判断王者是否升V2
                                // 查询下级是否有500单同时拥有3个V1
                                int total2 = customerDao.queryByOrderQuantityTotal(cu2.getId());
                                int rowSize2 = customerDao.queryByLevelV2(2, cu2.getId());
                                if(total2 >= 500){
                                    if(rowSize2 >= 3){
                                        cu2.setLevelCopy4Id(3);
                                        customerDao.update(cu2);

                                        if(cu2.getParentId() != null && cu2.getParentId() != 1198 && cu2.getLevelCopy4Id() != 8){
                                            Customer cu3 = customerDao.queryById(cu2.getParentId());
                                            if(cu3 != null){
                                                // 判断王者是否升V3
                                                // 查询下级是否有3000单同时拥有3个V2
                                                int total3 = customerDao.queryByOrderQuantityTotal(cu3.getId());
                                                int rowSize3 = customerDao.queryByLevelV3(3, cu3.getId());
                                                if(total3 >= 3000){
                                                    if(rowSize3 >= 3){
                                                        cu3.setLevelCopy4Id(4);
                                                        customerDao.update(cu3);

                                                        if(cu3.getParentId() != null && cu3.getParentId() != 1198 && cu3.getLevelCopy4Id() != 8) {
                                                            Customer cu4 = customerDao.queryById(cu3.getParentId());
                                                            if (cu4 != null) {
                                                                // 判断王者是否升V4
                                                                // 查询下级是否有9000单同时拥有3个V3
                                                                int total4 = customerDao.queryByOrderQuantityTotal(cu4.getId());
                                                                int rowSize4 = customerDao.queryByLevelV4(4, cu4.getId());
                                                                if(total4 >= 9000){
                                                                    if(rowSize4 >= 3){
                                                                        cu4.setLevelCopy4Id(5);
                                                                        customerDao.update(cu4);

                                                                        if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                            Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                            if(cu5 != null) {
                                                                                // 判断王者是否升V5
                                                                                // 查询下级是否有27000单同时拥有3个V4
                                                                                int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                if(total5 >= 27000){
                                                                                    if(rowSize5 >= 3){
                                                                                        cu5.setLevelCopy4Id(6);
                                                                                        customerDao.update(cu5);

                                                                                        if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                            Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                            if(cu6 != null) {
                                                                                                // 判断王者是否升V6
                                                                                                // 查询下级是否有81000单同时拥有3个V5
                                                                                                int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                if(total6 >= 81000){
                                                                                                    if(rowSize6 >= 3){
                                                                                                        cu6.setLevelCopy4Id(7);
                                                                                                        customerDao.update(cu6);

                                                                                                    }else {
                                                                                                        List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                        if (queryList6.size() > 0) {
                                                                                                            int index6 = rowSize6;
                                                                                                            for (Customer c6 : queryList6) {
                                                                                                                int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                if (temp6 > 0) {
                                                                                                                    c6.setLevelV5("1");
                                                                                                                    customerDao.update(c6);
                                                                                                                }
                                                                                                                index6 += temp6;
                                                                                                                if (index6 >= 3) {
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);
                                                                                                                    break;
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }

                                                                                    }else {
                                                                                        List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                        if (queryList5.size() > 0) {
                                                                                            int index5 = rowSize5;
                                                                                            for (Customer c5 : queryList5) {
                                                                                                int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                if (temp5 > 0) {
                                                                                                    c5.setLevelV4("1");
                                                                                                    customerDao.update(c5);
                                                                                                }
                                                                                                index5 += temp5;
                                                                                                if (index5 >= 3) {
                                                                                                    cu5.setLevelCopy4Id(6);
                                                                                                    customerDao.update(cu5);

                                                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                        if(cu6 != null) {
                                                                                                            // 判断王者是否升V6
                                                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                            if(total6 >= 81000){
                                                                                                                if(rowSize6 >= 3){
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);

                                                                                                                }else {
                                                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                    if (queryList6.size() > 0) {
                                                                                                                        int index6 = rowSize6;
                                                                                                                        for (Customer c6 : queryList6) {
                                                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                            if (temp6 > 0) {
                                                                                                                                c6.setLevelV5("1");
                                                                                                                                customerDao.update(c6);
                                                                                                                            }
                                                                                                                            index6 += temp6;
                                                                                                                            if (index6 >= 3) {
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);
                                                                                                                                break;
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                    break;
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }

                                                                    }else {
                                                                        List<Customer> queryList4 = customerDao.queryCustomerByLevelCopy4Id(4, cu4.getId());
                                                                        if (queryList4.size() > 0) {
                                                                            int index4 = rowSize4;
                                                                            for (Customer c4 : queryList4) {
                                                                                int temp4 = customerService.recursionCustomer_4(4, c4.getId());
                                                                                if (temp4 > 0) {
                                                                                    c4.setLevelV3("1");
                                                                                    customerDao.update(c4);
                                                                                }
                                                                                index4 += temp4;
                                                                                if (index4 >= 3) {
                                                                                    cu4.setLevelCopy4Id(5);
                                                                                    customerDao.update(cu4);

                                                                                    if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                                        Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                                        if(cu5 != null) {
                                                                                            // 判断王者是否升V5
                                                                                            // 查询下级是否有27000单同时拥有3个V4
                                                                                            int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                            int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                            if(total5 >= 27000){
                                                                                                if(rowSize5 >= 3){
                                                                                                    cu5.setLevelCopy4Id(6);
                                                                                                    customerDao.update(cu5);

                                                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                        if(cu6 != null) {
                                                                                                            // 判断王者是否升V6
                                                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                            if(total6 >= 81000){
                                                                                                                if(rowSize6 >= 3){
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);

                                                                                                                }else {
                                                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                    if (queryList6.size() > 0) {
                                                                                                                        int index6 = rowSize6;
                                                                                                                        for (Customer c6 : queryList6) {
                                                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                            if (temp6 > 0) {
                                                                                                                                c6.setLevelV5("1");
                                                                                                                                customerDao.update(c6);
                                                                                                                            }
                                                                                                                            index6 += temp6;
                                                                                                                            if (index6 >= 3) {
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);
                                                                                                                                break;
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }

                                                                                                }else {
                                                                                                    List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                                    if (queryList5.size() > 0) {
                                                                                                        int index5 = rowSize5;
                                                                                                        for (Customer c5 : queryList5) {
                                                                                                            int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                            if (temp5 > 0) {
                                                                                                                c5.setLevelV4("1");
                                                                                                                customerDao.update(c5);
                                                                                                            }
                                                                                                            index5 += temp5;
                                                                                                            if (index5 >= 3) {
                                                                                                                cu5.setLevelCopy4Id(6);
                                                                                                                customerDao.update(cu5);

                                                                                                                if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                    Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                    if(cu6 != null) {
                                                                                                                        // 判断王者是否升V6
                                                                                                                        // 查询下级是否有81000单同时拥有3个V5
                                                                                                                        int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                        int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                        if(total6 >= 81000){
                                                                                                                            if(rowSize6 >= 3){
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);

                                                                                                                            }else {
                                                                                                                                List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                if (queryList6.size() > 0) {
                                                                                                                                    int index6 = rowSize6;
                                                                                                                                    for (Customer c6 : queryList6) {
                                                                                                                                        int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                        if (temp6 > 0) {
                                                                                                                                            c6.setLevelV5("1");
                                                                                                                                            customerDao.update(c6);
                                                                                                                                        }
                                                                                                                                        index6 += temp6;
                                                                                                                                        if (index6 >= 3) {
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);
                                                                                                                                            break;
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                                break;
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    break;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }

                                                    }else {
                                                        List<Customer> queryList3 = customerDao.queryCustomerByLevelCopy4Id(3, cu3.getId());
                                                        if (queryList3.size() > 0) {
                                                            int index3 = rowSize3;
                                                            for (Customer c3 : queryList3) {
                                                                int temp3 = customerService.recursionCustomer_4(3, c3.getId());
                                                                if (temp3 > 0) {
                                                                    c3.setLevelV2("1");
                                                                    customerDao.update(c3);
                                                                }
                                                                index3 += temp3;
                                                                if (index3 >= 3) {
                                                                    cu3.setLevelCopy4Id(4);
                                                                    customerDao.update(cu3);

                                                                    if(cu3.getParentId() != null && cu3.getParentId() != 1198 && cu3.getLevelCopy4Id() != 8) {
                                                                        Customer cu4 = customerDao.queryById(cu3.getParentId());
                                                                        if (cu4 != null) {
                                                                            // 判断王者是否升V4
                                                                            // 查询下级是否有9000单同时拥有3个V3
                                                                            int total4 = customerDao.queryByOrderQuantityTotal(cu4.getId());
                                                                            int rowSize4 = customerDao.queryByLevelV4(4, cu4.getId());
                                                                            if(total4 >= 9000){
                                                                                if(rowSize4 >= 3){
                                                                                    cu4.setLevelCopy4Id(5);
                                                                                    customerDao.update(cu4);

                                                                                    if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                                        Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                                        if(cu5 != null) {
                                                                                            // 判断王者是否升V5
                                                                                            // 查询下级是否有27000单同时拥有3个V4
                                                                                            int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                            int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                            if(total5 >= 27000){
                                                                                                if(rowSize5 >= 3){
                                                                                                    cu5.setLevelCopy4Id(6);
                                                                                                    customerDao.update(cu5);

                                                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                        if(cu6 != null) {
                                                                                                            // 判断王者是否升V6
                                                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                            if(total6 >= 81000){
                                                                                                                if(rowSize6 >= 3){
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);

                                                                                                                }else {
                                                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                    if (queryList6.size() > 0) {
                                                                                                                        int index6 = rowSize6;
                                                                                                                        for (Customer c6 : queryList6) {
                                                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                            if (temp6 > 0) {
                                                                                                                                c6.setLevelV5("1");
                                                                                                                                customerDao.update(c6);
                                                                                                                            }
                                                                                                                            index6 += temp6;
                                                                                                                            if (index6 >= 3) {
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);
                                                                                                                                break;
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }

                                                                                                }else {
                                                                                                    List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                                    if (queryList5.size() > 0) {
                                                                                                        int index5 = rowSize5;
                                                                                                        for (Customer c5 : queryList5) {
                                                                                                            int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                            if (temp5 > 0) {
                                                                                                                c5.setLevelV4("1");
                                                                                                                customerDao.update(c5);
                                                                                                            }
                                                                                                            index5 += temp5;
                                                                                                            if (index5 >= 3) {
                                                                                                                cu5.setLevelCopy4Id(6);
                                                                                                                customerDao.update(cu5);

                                                                                                                if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                    Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                    if(cu6 != null) {
                                                                                                                        // 判断王者是否升V6
                                                                                                                        // 查询下级是否有81000单同时拥有3个V5
                                                                                                                        int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                        int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                        if(total6 >= 81000){
                                                                                                                            if(rowSize6 >= 3){
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);

                                                                                                                            }else {
                                                                                                                                List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                if (queryList6.size() > 0) {
                                                                                                                                    int index6 = rowSize6;
                                                                                                                                    for (Customer c6 : queryList6) {
                                                                                                                                        int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                        if (temp6 > 0) {
                                                                                                                                            c6.setLevelV5("1");
                                                                                                                                            customerDao.update(c6);
                                                                                                                                        }
                                                                                                                                        index6 += temp6;
                                                                                                                                        if (index6 >= 3) {
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);
                                                                                                                                            break;
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                                break;
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }

                                                                                }else {
                                                                                    List<Customer> queryList4 = customerDao.queryCustomerByLevelCopy4Id(4, cu4.getId());
                                                                                    if (queryList4.size() > 0) {
                                                                                        int index4 = rowSize4;
                                                                                        for (Customer c4 : queryList4) {
                                                                                            int temp4 = customerService.recursionCustomer_4(4, c4.getId());
                                                                                            if (temp4 > 0) {
                                                                                                c4.setLevelV3("1");
                                                                                                customerDao.update(c4);
                                                                                            }
                                                                                            index4 += temp4;
                                                                                            if (index4 >= 3) {
                                                                                                cu4.setLevelCopy4Id(5);
                                                                                                customerDao.update(cu4);

                                                                                                if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                                                    Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                                                    if(cu5 != null) {
                                                                                                        // 判断王者是否升V5
                                                                                                        // 查询下级是否有27000单同时拥有3个V4
                                                                                                        int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                                        int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                                        if(total5 >= 27000){
                                                                                                            if(rowSize5 >= 3){
                                                                                                                cu5.setLevelCopy4Id(6);
                                                                                                                customerDao.update(cu5);

                                                                                                                if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                    Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                    if(cu6 != null) {
                                                                                                                        // 判断王者是否升V6
                                                                                                                        // 查询下级是否有81000单同时拥有3个V5
                                                                                                                        int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                        int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                        if(total6 >= 81000){
                                                                                                                            if(rowSize6 >= 3){
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);

                                                                                                                            }else {
                                                                                                                                List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                if (queryList6.size() > 0) {
                                                                                                                                    int index6 = rowSize6;
                                                                                                                                    for (Customer c6 : queryList6) {
                                                                                                                                        int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                        if (temp6 > 0) {
                                                                                                                                            c6.setLevelV5("1");
                                                                                                                                            customerDao.update(c6);
                                                                                                                                        }
                                                                                                                                        index6 += temp6;
                                                                                                                                        if (index6 >= 3) {
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);
                                                                                                                                            break;
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }

                                                                                                            }else {
                                                                                                                List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                                                if (queryList5.size() > 0) {
                                                                                                                    int index5 = rowSize5;
                                                                                                                    for (Customer c5 : queryList5) {
                                                                                                                        int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                                        if (temp5 > 0) {
                                                                                                                            c5.setLevelV4("1");
                                                                                                                            customerDao.update(c5);
                                                                                                                        }
                                                                                                                        index5 += temp5;
                                                                                                                        if (index5 >= 3) {
                                                                                                                            cu5.setLevelCopy4Id(6);
                                                                                                                            customerDao.update(cu5);

                                                                                                                            if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                                Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                                if(cu6 != null) {
                                                                                                                                    // 判断王者是否升V6
                                                                                                                                    // 查询下级是否有81000单同时拥有3个V5
                                                                                                                                    int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                                    int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                                    if(total6 >= 81000){
                                                                                                                                        if(rowSize6 >= 3){
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);

                                                                                                                                        }else {
                                                                                                                                            List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                            if (queryList6.size() > 0) {
                                                                                                                                                int index6 = rowSize6;
                                                                                                                                                for (Customer c6 : queryList6) {
                                                                                                                                                    int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                                    if (temp6 > 0) {
                                                                                                                                                        c6.setLevelV5("1");
                                                                                                                                                        customerDao.update(c6);
                                                                                                                                                    }
                                                                                                                                                    index6 += temp6;
                                                                                                                                                    if (index6 >= 3) {
                                                                                                                                                        cu6.setLevelCopy4Id(7);
                                                                                                                                                        customerDao.update(cu6);
                                                                                                                                                        break;
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                            break;
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                                break;
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                    }else {
                                        List<Customer> queryList2 = customerDao.queryCustomerByLevelCopy4Id(2, cu2.getId());
                                        if (queryList2.size() > 0) {
                                            int index2 = rowSize2;
                                            for (Customer c2 : queryList2) {
                                                int temp2 = customerService.recursionCustomer_4(2, c2.getId());
                                                if (temp2 > 0) {
                                                    c2.setLevelV1("1");
                                                    customerDao.update(c2);
                                                }
                                                index2 += temp2;
                                                if (index2 >= 3) {
                                                    cu2.setLevelCopy4Id(3);
                                                    customerDao.update(cu2);

                                                    if(cu2.getParentId() != null && cu2.getParentId() != 1198 && cu2.getLevelCopy4Id() != 8){
                                                        Customer cu3 = customerDao.queryById(cu2.getParentId());
                                                        if(cu3 != null){
                                                            // 判断王者是否升V3
                                                            // 查询下级是否有3000单同时拥有3个V2
                                                            int total3 = customerDao.queryByOrderQuantityTotal(cu3.getId());
                                                            int rowSize3 = customerDao.queryByLevelV3(3, cu3.getId());
                                                            if(total3 >= 3000){
                                                                if(rowSize3 >= 3){
                                                                    cu3.setLevelCopy4Id(4);
                                                                    customerDao.update(cu3);

                                                                    if(cu3.getParentId() != null && cu3.getParentId() != 1198 && cu3.getLevelCopy4Id() != 8) {
                                                                        Customer cu4 = customerDao.queryById(cu3.getParentId());
                                                                        if (cu4 != null) {
                                                                            // 判断王者是否升V4
                                                                            // 查询下级是否有9000单同时拥有3个V3
                                                                            int total4 = customerDao.queryByOrderQuantityTotal(cu4.getId());
                                                                            int rowSize4 = customerDao.queryByLevelV4(4, cu4.getId());
                                                                            if(total4 >= 9000){
                                                                                if(rowSize4 >= 3){
                                                                                    cu4.setLevelCopy4Id(5);
                                                                                    customerDao.update(cu4);

                                                                                    if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                                        Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                                        if(cu5 != null) {
                                                                                            // 判断王者是否升V5
                                                                                            // 查询下级是否有27000单同时拥有3个V4
                                                                                            int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                            int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                            if(total5 >= 27000){
                                                                                                if(rowSize5 >= 3){
                                                                                                    cu5.setLevelCopy4Id(6);
                                                                                                    customerDao.update(cu5);

                                                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                        if(cu6 != null) {
                                                                                                            // 判断王者是否升V6
                                                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                            if(total6 >= 81000){
                                                                                                                if(rowSize6 >= 3){
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);

                                                                                                                }else {
                                                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                    if (queryList6.size() > 0) {
                                                                                                                        int index6 = rowSize6;
                                                                                                                        for (Customer c6 : queryList6) {
                                                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                            if (temp6 > 0) {
                                                                                                                                c6.setLevelV5("1");
                                                                                                                                customerDao.update(c6);
                                                                                                                            }
                                                                                                                            index6 += temp6;
                                                                                                                            if (index6 >= 3) {
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);
                                                                                                                                break;
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }

                                                                                                }else {
                                                                                                    List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                                    if (queryList5.size() > 0) {
                                                                                                        int index5 = rowSize5;
                                                                                                        for (Customer c5 : queryList5) {
                                                                                                            int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                            if (temp5 > 0) {
                                                                                                                c5.setLevelV4("1");
                                                                                                                customerDao.update(c5);
                                                                                                            }
                                                                                                            index5 += temp5;
                                                                                                            if (index5 >= 3) {
                                                                                                                cu5.setLevelCopy4Id(6);
                                                                                                                customerDao.update(cu5);

                                                                                                                if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                    Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                    if(cu6 != null) {
                                                                                                                        // 判断王者是否升V6
                                                                                                                        // 查询下级是否有81000单同时拥有3个V5
                                                                                                                        int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                        int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                        if(total6 >= 81000){
                                                                                                                            if(rowSize6 >= 3){
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);

                                                                                                                            }else {
                                                                                                                                List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                if (queryList6.size() > 0) {
                                                                                                                                    int index6 = rowSize6;
                                                                                                                                    for (Customer c6 : queryList6) {
                                                                                                                                        int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                        if (temp6 > 0) {
                                                                                                                                            c6.setLevelV5("1");
                                                                                                                                            customerDao.update(c6);
                                                                                                                                        }
                                                                                                                                        index6 += temp6;
                                                                                                                                        if (index6 >= 3) {
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);
                                                                                                                                            break;
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                                break;
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }

                                                                                }else {
                                                                                    List<Customer> queryList4 = customerDao.queryCustomerByLevelCopy4Id(4, cu4.getId());
                                                                                    if (queryList4.size() > 0) {
                                                                                        int index4 = rowSize4;
                                                                                        for (Customer c4 : queryList4) {
                                                                                            int temp4 = customerService.recursionCustomer_4(4, c4.getId());
                                                                                            if (temp4 > 0) {
                                                                                                c4.setLevelV3("1");
                                                                                                customerDao.update(c4);
                                                                                            }
                                                                                            index4 += temp4;
                                                                                            if (index4 >= 3) {
                                                                                                cu4.setLevelCopy4Id(5);
                                                                                                customerDao.update(cu4);

                                                                                                if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                                                    Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                                                    if(cu5 != null) {
                                                                                                        // 判断王者是否升V5
                                                                                                        // 查询下级是否有27000单同时拥有3个V4
                                                                                                        int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                                        int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                                        if(total5 >= 27000){
                                                                                                            if(rowSize5 >= 3){
                                                                                                                cu5.setLevelCopy4Id(6);
                                                                                                                customerDao.update(cu5);

                                                                                                                if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                    Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                    if(cu6 != null) {
                                                                                                                        // 判断王者是否升V6
                                                                                                                        // 查询下级是否有81000单同时拥有3个V5
                                                                                                                        int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                        int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                        if(total6 >= 81000){
                                                                                                                            if(rowSize6 >= 3){
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);

                                                                                                                            }else {
                                                                                                                                List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                if (queryList6.size() > 0) {
                                                                                                                                    int index6 = rowSize6;
                                                                                                                                    for (Customer c6 : queryList6) {
                                                                                                                                        int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                        if (temp6 > 0) {
                                                                                                                                            c6.setLevelV5("1");
                                                                                                                                            customerDao.update(c6);
                                                                                                                                        }
                                                                                                                                        index6 += temp6;
                                                                                                                                        if (index6 >= 3) {
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);
                                                                                                                                            break;
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }

                                                                                                            }else {
                                                                                                                List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                                                if (queryList5.size() > 0) {
                                                                                                                    int index5 = rowSize5;
                                                                                                                    for (Customer c5 : queryList5) {
                                                                                                                        int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                                        if (temp5 > 0) {
                                                                                                                            c5.setLevelV4("1");
                                                                                                                            customerDao.update(c5);
                                                                                                                        }
                                                                                                                        index5 += temp5;
                                                                                                                        if (index5 >= 3) {
                                                                                                                            cu5.setLevelCopy4Id(6);
                                                                                                                            customerDao.update(cu5);

                                                                                                                            if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                                Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                                if(cu6 != null) {
                                                                                                                                    // 判断王者是否升V6
                                                                                                                                    // 查询下级是否有81000单同时拥有3个V5
                                                                                                                                    int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                                    int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                                    if(total6 >= 81000){
                                                                                                                                        if(rowSize6 >= 3){
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);

                                                                                                                                        }else {
                                                                                                                                            List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                            if (queryList6.size() > 0) {
                                                                                                                                                int index6 = rowSize6;
                                                                                                                                                for (Customer c6 : queryList6) {
                                                                                                                                                    int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                                    if (temp6 > 0) {
                                                                                                                                                        c6.setLevelV5("1");
                                                                                                                                                        customerDao.update(c6);
                                                                                                                                                    }
                                                                                                                                                    index6 += temp6;
                                                                                                                                                    if (index6 >= 3) {
                                                                                                                                                        cu6.setLevelCopy4Id(7);
                                                                                                                                                        customerDao.update(cu6);
                                                                                                                                                        break;
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                            break;
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                                break;
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }

                                                                }else {
                                                                    List<Customer> queryList3 = customerDao.queryCustomerByLevelCopy4Id(3, cu3.getId());
                                                                    if (queryList3.size() > 0) {
                                                                        int index3 = rowSize3;
                                                                        for (Customer c3 : queryList3) {
                                                                            int temp3 = customerService.recursionCustomer_4(3, c3.getId());
                                                                            if (temp3 > 0) {
                                                                                c3.setLevelV2("1");
                                                                                customerDao.update(c3);
                                                                            }
                                                                            index3 += temp3;
                                                                            if (index3 >= 3) {
                                                                                cu3.setLevelCopy4Id(4);
                                                                                customerDao.update(cu3);

                                                                                if(cu3.getParentId() != null && cu3.getParentId() != 1198 && cu3.getLevelCopy4Id() != 8) {
                                                                                    Customer cu4 = customerDao.queryById(cu3.getParentId());
                                                                                    if (cu4 != null) {
                                                                                        // 判断王者是否升V4
                                                                                        // 查询下级是否有9000单同时拥有3个V3
                                                                                        int total4 = customerDao.queryByOrderQuantityTotal(cu4.getId());
                                                                                        int rowSize4 = customerDao.queryByLevelV4(4, cu4.getId());
                                                                                        if(total4 >= 9000){
                                                                                            if(rowSize4 >= 3){
                                                                                                cu4.setLevelCopy4Id(5);
                                                                                                customerDao.update(cu4);

                                                                                                if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                                                    Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                                                    if(cu5 != null) {
                                                                                                        // 判断王者是否升V5
                                                                                                        // 查询下级是否有27000单同时拥有3个V4
                                                                                                        int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                                        int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                                        if(total5 >= 27000){
                                                                                                            if(rowSize5 >= 3){
                                                                                                                cu5.setLevelCopy4Id(6);
                                                                                                                customerDao.update(cu5);

                                                                                                                if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                    Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                    if(cu6 != null) {
                                                                                                                        // 判断王者是否升V6
                                                                                                                        // 查询下级是否有81000单同时拥有3个V5
                                                                                                                        int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                        int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                        if(total6 >= 81000){
                                                                                                                            if(rowSize6 >= 3){
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);

                                                                                                                            }else {
                                                                                                                                List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                if (queryList6.size() > 0) {
                                                                                                                                    int index6 = rowSize6;
                                                                                                                                    for (Customer c6 : queryList6) {
                                                                                                                                        int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                        if (temp6 > 0) {
                                                                                                                                            c6.setLevelV5("1");
                                                                                                                                            customerDao.update(c6);
                                                                                                                                        }
                                                                                                                                        index6 += temp6;
                                                                                                                                        if (index6 >= 3) {
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);
                                                                                                                                            break;
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }

                                                                                                            }else {
                                                                                                                List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                                                if (queryList5.size() > 0) {
                                                                                                                    int index5 = rowSize5;
                                                                                                                    for (Customer c5 : queryList5) {
                                                                                                                        int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                                        if (temp5 > 0) {
                                                                                                                            c5.setLevelV4("1");
                                                                                                                            customerDao.update(c5);
                                                                                                                        }
                                                                                                                        index5 += temp5;
                                                                                                                        if (index5 >= 3) {
                                                                                                                            cu5.setLevelCopy4Id(6);
                                                                                                                            customerDao.update(cu5);

                                                                                                                            if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                                Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                                if(cu6 != null) {
                                                                                                                                    // 判断王者是否升V6
                                                                                                                                    // 查询下级是否有81000单同时拥有3个V5
                                                                                                                                    int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                                    int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                                    if(total6 >= 81000){
                                                                                                                                        if(rowSize6 >= 3){
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);

                                                                                                                                        }else {
                                                                                                                                            List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                            if (queryList6.size() > 0) {
                                                                                                                                                int index6 = rowSize6;
                                                                                                                                                for (Customer c6 : queryList6) {
                                                                                                                                                    int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                                    if (temp6 > 0) {
                                                                                                                                                        c6.setLevelV5("1");
                                                                                                                                                        customerDao.update(c6);
                                                                                                                                                    }
                                                                                                                                                    index6 += temp6;
                                                                                                                                                    if (index6 >= 3) {
                                                                                                                                                        cu6.setLevelCopy4Id(7);
                                                                                                                                                        customerDao.update(cu6);
                                                                                                                                                        break;
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                            break;
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }

                                                                                            }else {
                                                                                                List<Customer> queryList4 = customerDao.queryCustomerByLevelCopy4Id(4, cu4.getId());
                                                                                                if (queryList4.size() > 0) {
                                                                                                    int index4 = rowSize4;
                                                                                                    for (Customer c4 : queryList4) {
                                                                                                        int temp4 = customerService.recursionCustomer_4(4, c4.getId());
                                                                                                        if (temp4 > 0) {
                                                                                                            c4.setLevelV3("1");
                                                                                                            customerDao.update(c4);
                                                                                                        }
                                                                                                        index4 += temp4;
                                                                                                        if (index4 >= 3) {
                                                                                                            cu4.setLevelCopy4Id(5);
                                                                                                            customerDao.update(cu4);

                                                                                                            if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                                                                Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                                                                if(cu5 != null) {
                                                                                                                    // 判断王者是否升V5
                                                                                                                    // 查询下级是否有27000单同时拥有3个V4
                                                                                                                    int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                                                    int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                                                    if(total5 >= 27000){
                                                                                                                        if(rowSize5 >= 3){
                                                                                                                            cu5.setLevelCopy4Id(6);
                                                                                                                            customerDao.update(cu5);

                                                                                                                            if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                                Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                                if(cu6 != null) {
                                                                                                                                    // 判断王者是否升V6
                                                                                                                                    // 查询下级是否有81000单同时拥有3个V5
                                                                                                                                    int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                                    int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                                    if(total6 >= 81000){
                                                                                                                                        if(rowSize6 >= 3){
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);

                                                                                                                                        }else {
                                                                                                                                            List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                            if (queryList6.size() > 0) {
                                                                                                                                                int index6 = rowSize6;
                                                                                                                                                for (Customer c6 : queryList6) {
                                                                                                                                                    int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                                    if (temp6 > 0) {
                                                                                                                                                        c6.setLevelV5("1");
                                                                                                                                                        customerDao.update(c6);
                                                                                                                                                    }
                                                                                                                                                    index6 += temp6;
                                                                                                                                                    if (index6 >= 3) {
                                                                                                                                                        cu6.setLevelCopy4Id(7);
                                                                                                                                                        customerDao.update(cu6);
                                                                                                                                                        break;
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }

                                                                                                                        }else {
                                                                                                                            List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                                                            if (queryList5.size() > 0) {
                                                                                                                                int index5 = rowSize5;
                                                                                                                                for (Customer c5 : queryList5) {
                                                                                                                                    int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                                                    if (temp5 > 0) {
                                                                                                                                        c5.setLevelV4("1");
                                                                                                                                        customerDao.update(c5);
                                                                                                                                    }
                                                                                                                                    index5 += temp5;
                                                                                                                                    if (index5 >= 3) {
                                                                                                                                        cu5.setLevelCopy4Id(6);
                                                                                                                                        customerDao.update(cu5);

                                                                                                                                        if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                                            Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                                            if(cu6 != null) {
                                                                                                                                                // 判断王者是否升V6
                                                                                                                                                // 查询下级是否有81000单同时拥有3个V5
                                                                                                                                                int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                                                int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                                                if(total6 >= 81000){
                                                                                                                                                    if(rowSize6 >= 3){
                                                                                                                                                        cu6.setLevelCopy4Id(7);
                                                                                                                                                        customerDao.update(cu6);

                                                                                                                                                    }else {
                                                                                                                                                        List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                                        if (queryList6.size() > 0) {
                                                                                                                                                            int index6 = rowSize6;
                                                                                                                                                            for (Customer c6 : queryList6) {
                                                                                                                                                                int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                                                if (temp6 > 0) {
                                                                                                                                                                    c6.setLevelV5("1");
                                                                                                                                                                    customerDao.update(c6);
                                                                                                                                                                }
                                                                                                                                                                index6 += temp6;
                                                                                                                                                                if (index6 >= 3) {
                                                                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                                                                    customerDao.update(cu6);
                                                                                                                                                                    break;
                                                                                                                                                                }
                                                                                                                                                            }
                                                                                                                                                        }
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                        break;
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                            break;
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                                break;
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                }else if(cu1.getLevelCopy4Id() == 2){ //v1
                    // 判断是否升V2
                    // 查询下级是否有500单同时拥有3个V1
                    int total2 = customerDao.queryByOrderQuantityTotal(cu1.getId());
                    int rowSize2 = customerDao.queryByLevelV2(2, cu1.getId());
                    if(total2 >= 500){
                        if(rowSize2 >= 3){
                            cu1.setLevelCopy4Id(3);
                            customerDao.update(cu1);

                            if(cu1.getParentId() != null && cu1.getParentId() != 1198 && cu1.getLevelCopy4Id() != 8){
                                Customer cu3 = customerDao.queryById(cu1.getParentId());
                                if(cu3 != null){
                                    // 判断王者是否升V3
                                    // 查询下级是否有3000单同时拥有3个V2
                                    int total3 = customerDao.queryByOrderQuantityTotal(cu3.getId());
                                    int rowSize3 = customerDao.queryByLevelV3(3, cu3.getId());
                                    if(total3 >= 3000){
                                        if(rowSize3 >= 3){
                                            cu3.setLevelCopy4Id(4);
                                            customerDao.update(cu3);

                                            if(cu3.getParentId() != null && cu3.getParentId() != 1198 && cu3.getLevelCopy4Id() != 8) {
                                                Customer cu4 = customerDao.queryById(cu3.getParentId());
                                                if (cu4 != null) {
                                                    // 判断王者是否升V4
                                                    // 查询下级是否有9000单同时拥有3个V3
                                                    int total4 = customerDao.queryByOrderQuantityTotal(cu4.getId());
                                                    int rowSize4 = customerDao.queryByLevelV4(4, cu4.getId());
                                                    if(total4 >= 9000){
                                                        if(rowSize4 >= 3){
                                                            cu4.setLevelCopy4Id(5);
                                                            customerDao.update(cu4);

                                                            if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                if(cu5 != null) {
                                                                    // 判断王者是否升V5
                                                                    // 查询下级是否有27000单同时拥有3个V4
                                                                    int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                    int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                    if(total5 >= 27000){
                                                                        if(rowSize5 >= 3){
                                                                            cu5.setLevelCopy4Id(6);
                                                                            customerDao.update(cu5);

                                                                            if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                if(cu6 != null) {
                                                                                    // 判断王者是否升V6
                                                                                    // 查询下级是否有81000单同时拥有3个V5
                                                                                    int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                    int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                    if(total6 >= 81000){
                                                                                        if(rowSize6 >= 3){
                                                                                            cu6.setLevelCopy4Id(7);
                                                                                            customerDao.update(cu6);

                                                                                        }else {
                                                                                            List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                            if (queryList6.size() > 0) {
                                                                                                int index6 = rowSize6;
                                                                                                for (Customer c6 : queryList6) {
                                                                                                    int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                    if (temp6 > 0) {
                                                                                                        c6.setLevelV5("1");
                                                                                                        customerDao.update(c6);
                                                                                                    }
                                                                                                    index6 += temp6;
                                                                                                    if (index6 >= 3) {
                                                                                                        cu6.setLevelCopy4Id(7);
                                                                                                        customerDao.update(cu6);
                                                                                                        break;
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }

                                                                        }else {
                                                                            List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                            if (queryList5.size() > 0) {
                                                                                int index5 = rowSize5;
                                                                                for (Customer c5 : queryList5) {
                                                                                    int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                    if (temp5 > 0) {
                                                                                        c5.setLevelV4("1");
                                                                                        customerDao.update(c5);
                                                                                    }
                                                                                    index5 += temp5;
                                                                                    if (index5 >= 3) {
                                                                                        cu5.setLevelCopy4Id(6);
                                                                                        customerDao.update(cu5);

                                                                                        if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                            Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                            if(cu6 != null) {
                                                                                                // 判断王者是否升V6
                                                                                                // 查询下级是否有81000单同时拥有3个V5
                                                                                                int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                if(total6 >= 81000){
                                                                                                    if(rowSize6 >= 3){
                                                                                                        cu6.setLevelCopy4Id(7);
                                                                                                        customerDao.update(cu6);

                                                                                                    }else {
                                                                                                        List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                        if (queryList6.size() > 0) {
                                                                                                            int index6 = rowSize6;
                                                                                                            for (Customer c6 : queryList6) {
                                                                                                                int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                if (temp6 > 0) {
                                                                                                                    c6.setLevelV5("1");
                                                                                                                    customerDao.update(c6);
                                                                                                                }
                                                                                                                index6 += temp6;
                                                                                                                if (index6 >= 3) {
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);
                                                                                                                    break;
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                        break;
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }

                                                        }else {
                                                            List<Customer> queryList4 = customerDao.queryCustomerByLevelCopy4Id(4, cu4.getId());
                                                            if (queryList4.size() > 0) {
                                                                int index4 = rowSize4;
                                                                for (Customer c4 : queryList4) {
                                                                    int temp4 = customerService.recursionCustomer_4(4, c4.getId());
                                                                    if (temp4 > 0) {
                                                                        c4.setLevelV3("1");
                                                                        customerDao.update(c4);
                                                                    }
                                                                    index4 += temp4;
                                                                    if (index4 >= 3) {
                                                                        cu4.setLevelCopy4Id(5);
                                                                        customerDao.update(cu4);

                                                                        if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                            Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                            if(cu5 != null) {
                                                                                // 判断王者是否升V5
                                                                                // 查询下级是否有27000单同时拥有3个V4
                                                                                int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                if(total5 >= 27000){
                                                                                    if(rowSize5 >= 3){
                                                                                        cu5.setLevelCopy4Id(6);
                                                                                        customerDao.update(cu5);

                                                                                        if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                            Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                            if(cu6 != null) {
                                                                                                // 判断王者是否升V6
                                                                                                // 查询下级是否有81000单同时拥有3个V5
                                                                                                int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                if(total6 >= 81000){
                                                                                                    if(rowSize6 >= 3){
                                                                                                        cu6.setLevelCopy4Id(7);
                                                                                                        customerDao.update(cu6);

                                                                                                    }else {
                                                                                                        List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                        if (queryList6.size() > 0) {
                                                                                                            int index6 = rowSize6;
                                                                                                            for (Customer c6 : queryList6) {
                                                                                                                int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                if (temp6 > 0) {
                                                                                                                    c6.setLevelV5("1");
                                                                                                                    customerDao.update(c6);
                                                                                                                }
                                                                                                                index6 += temp6;
                                                                                                                if (index6 >= 3) {
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);
                                                                                                                    break;
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }

                                                                                    }else {
                                                                                        List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                        if (queryList5.size() > 0) {
                                                                                            int index5 = rowSize5;
                                                                                            for (Customer c5 : queryList5) {
                                                                                                int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                if (temp5 > 0) {
                                                                                                    c5.setLevelV4("1");
                                                                                                    customerDao.update(c5);
                                                                                                }
                                                                                                index5 += temp5;
                                                                                                if (index5 >= 3) {
                                                                                                    cu5.setLevelCopy4Id(6);
                                                                                                    customerDao.update(cu5);

                                                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                        if(cu6 != null) {
                                                                                                            // 判断王者是否升V6
                                                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                            if(total6 >= 81000){
                                                                                                                if(rowSize6 >= 3){
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);

                                                                                                                }else {
                                                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                    if (queryList6.size() > 0) {
                                                                                                                        int index6 = rowSize6;
                                                                                                                        for (Customer c6 : queryList6) {
                                                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                            if (temp6 > 0) {
                                                                                                                                c6.setLevelV5("1");
                                                                                                                                customerDao.update(c6);
                                                                                                                            }
                                                                                                                            index6 += temp6;
                                                                                                                            if (index6 >= 3) {
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);
                                                                                                                                break;
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                    break;
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }

                                        }else {
                                            List<Customer> queryList3 = customerDao.queryCustomerByLevelCopy4Id(3, cu3.getId());
                                            if (queryList3.size() > 0) {
                                                int index3 = rowSize3;
                                                for (Customer c3 : queryList3) {
                                                    int temp3 = customerService.recursionCustomer_4(3, c3.getId());
                                                    if (temp3 > 0) {
                                                        c3.setLevelV2("1");
                                                        customerDao.update(c3);
                                                    }
                                                    index3 += temp3;
                                                    if (index3 >= 3) {
                                                        cu3.setLevelCopy4Id(4);
                                                        customerDao.update(cu3);

                                                        if(cu3.getParentId() != null && cu3.getParentId() != 1198 && cu3.getLevelCopy4Id() != 8) {
                                                            Customer cu4 = customerDao.queryById(cu3.getParentId());
                                                            if (cu4 != null) {
                                                                // 判断王者是否升V4
                                                                // 查询下级是否有9000单同时拥有3个V3
                                                                int total4 = customerDao.queryByOrderQuantityTotal(cu4.getId());
                                                                int rowSize4 = customerDao.queryByLevelV4(4, cu4.getId());
                                                                if(total4 >= 9000){
                                                                    if(rowSize4 >= 3){
                                                                        cu4.setLevelCopy4Id(5);
                                                                        customerDao.update(cu4);

                                                                        if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                            Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                            if(cu5 != null) {
                                                                                // 判断王者是否升V5
                                                                                // 查询下级是否有27000单同时拥有3个V4
                                                                                int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                if(total5 >= 27000){
                                                                                    if(rowSize5 >= 3){
                                                                                        cu5.setLevelCopy4Id(6);
                                                                                        customerDao.update(cu5);

                                                                                        if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                            Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                            if(cu6 != null) {
                                                                                                // 判断王者是否升V6
                                                                                                // 查询下级是否有81000单同时拥有3个V5
                                                                                                int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                if(total6 >= 81000){
                                                                                                    if(rowSize6 >= 3){
                                                                                                        cu6.setLevelCopy4Id(7);
                                                                                                        customerDao.update(cu6);

                                                                                                    }else {
                                                                                                        List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                        if (queryList6.size() > 0) {
                                                                                                            int index6 = rowSize6;
                                                                                                            for (Customer c6 : queryList6) {
                                                                                                                int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                if (temp6 > 0) {
                                                                                                                    c6.setLevelV5("1");
                                                                                                                    customerDao.update(c6);
                                                                                                                }
                                                                                                                index6 += temp6;
                                                                                                                if (index6 >= 3) {
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);
                                                                                                                    break;
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }

                                                                                    }else {
                                                                                        List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                        if (queryList5.size() > 0) {
                                                                                            int index5 = rowSize5;
                                                                                            for (Customer c5 : queryList5) {
                                                                                                int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                if (temp5 > 0) {
                                                                                                    c5.setLevelV4("1");
                                                                                                    customerDao.update(c5);
                                                                                                }
                                                                                                index5 += temp5;
                                                                                                if (index5 >= 3) {
                                                                                                    cu5.setLevelCopy4Id(6);
                                                                                                    customerDao.update(cu5);

                                                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                        if(cu6 != null) {
                                                                                                            // 判断王者是否升V6
                                                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                            if(total6 >= 81000){
                                                                                                                if(rowSize6 >= 3){
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);

                                                                                                                }else {
                                                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                    if (queryList6.size() > 0) {
                                                                                                                        int index6 = rowSize6;
                                                                                                                        for (Customer c6 : queryList6) {
                                                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                            if (temp6 > 0) {
                                                                                                                                c6.setLevelV5("1");
                                                                                                                                customerDao.update(c6);
                                                                                                                            }
                                                                                                                            index6 += temp6;
                                                                                                                            if (index6 >= 3) {
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);
                                                                                                                                break;
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                    break;
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }

                                                                    }else {
                                                                        List<Customer> queryList4 = customerDao.queryCustomerByLevelCopy4Id(4, cu4.getId());
                                                                        if (queryList4.size() > 0) {
                                                                            int index4 = rowSize4;
                                                                            for (Customer c4 : queryList4) {
                                                                                int temp4 = customerService.recursionCustomer_4(4, c4.getId());
                                                                                if (temp4 > 0) {
                                                                                    c4.setLevelV3("1");
                                                                                    customerDao.update(c4);
                                                                                }
                                                                                index4 += temp4;
                                                                                if (index4 >= 3) {
                                                                                    cu4.setLevelCopy4Id(5);
                                                                                    customerDao.update(cu4);

                                                                                    if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                                        Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                                        if(cu5 != null) {
                                                                                            // 判断王者是否升V5
                                                                                            // 查询下级是否有27000单同时拥有3个V4
                                                                                            int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                            int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                            if(total5 >= 27000){
                                                                                                if(rowSize5 >= 3){
                                                                                                    cu5.setLevelCopy4Id(6);
                                                                                                    customerDao.update(cu5);

                                                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                        if(cu6 != null) {
                                                                                                            // 判断王者是否升V6
                                                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                            if(total6 >= 81000){
                                                                                                                if(rowSize6 >= 3){
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);

                                                                                                                }else {
                                                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                    if (queryList6.size() > 0) {
                                                                                                                        int index6 = rowSize6;
                                                                                                                        for (Customer c6 : queryList6) {
                                                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                            if (temp6 > 0) {
                                                                                                                                c6.setLevelV5("1");
                                                                                                                                customerDao.update(c6);
                                                                                                                            }
                                                                                                                            index6 += temp6;
                                                                                                                            if (index6 >= 3) {
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);
                                                                                                                                break;
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }

                                                                                                }else {
                                                                                                    List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                                    if (queryList5.size() > 0) {
                                                                                                        int index5 = rowSize5;
                                                                                                        for (Customer c5 : queryList5) {
                                                                                                            int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                            if (temp5 > 0) {
                                                                                                                c5.setLevelV4("1");
                                                                                                                customerDao.update(c5);
                                                                                                            }
                                                                                                            index5 += temp5;
                                                                                                            if (index5 >= 3) {
                                                                                                                cu5.setLevelCopy4Id(6);
                                                                                                                customerDao.update(cu5);

                                                                                                                if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                    Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                    if(cu6 != null) {
                                                                                                                        // 判断王者是否升V6
                                                                                                                        // 查询下级是否有81000单同时拥有3个V5
                                                                                                                        int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                        int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                        if(total6 >= 81000){
                                                                                                                            if(rowSize6 >= 3){
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);

                                                                                                                            }else {
                                                                                                                                List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                if (queryList6.size() > 0) {
                                                                                                                                    int index6 = rowSize6;
                                                                                                                                    for (Customer c6 : queryList6) {
                                                                                                                                        int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                        if (temp6 > 0) {
                                                                                                                                            c6.setLevelV5("1");
                                                                                                                                            customerDao.update(c6);
                                                                                                                                        }
                                                                                                                                        index6 += temp6;
                                                                                                                                        if (index6 >= 3) {
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);
                                                                                                                                            break;
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                                break;
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    break;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }else {
                            List<Customer> queryList2 = customerDao.queryCustomerByLevelCopy4Id(2, cu1.getId());
                            if (queryList2.size() > 0) {
                                int index2 = rowSize2;
                                for (Customer c2 : queryList2) {
                                    int temp2 = customerService.recursionCustomer_4(2, c2.getId());
                                    if (temp2 > 0) {
                                        c2.setLevelV1("1");
                                        customerDao.update(c2);
                                    }
                                    index2 += temp2;
                                    if (index2 >= 3) {
                                        cu1.setLevelCopy4Id(3);
                                        customerDao.update(cu1);

                                        if(cu1.getParentId() != null && cu1.getParentId() != 1198 && cu1.getLevelCopy4Id() != 8){
                                            Customer cu3 = customerDao.queryById(cu1.getParentId());
                                            if(cu3 != null){
                                                // 判断王者是否升V3
                                                // 查询下级是否有3000单同时拥有3个V2
                                                int total3 = customerDao.queryByOrderQuantityTotal(cu3.getId());
                                                int rowSize3 = customerDao.queryByLevelV3(3, cu3.getId());
                                                if(total3 >= 3000){
                                                    if(rowSize3 >= 3){
                                                        cu3.setLevelCopy4Id(4);
                                                        customerDao.update(cu3);

                                                        if(cu3.getParentId() != null && cu3.getParentId() != 1198 && cu3.getLevelCopy4Id() != 8) {
                                                            Customer cu4 = customerDao.queryById(cu3.getParentId());
                                                            if (cu4 != null) {
                                                                // 判断王者是否升V4
                                                                // 查询下级是否有9000单同时拥有3个V3
                                                                int total4 = customerDao.queryByOrderQuantityTotal(cu4.getId());
                                                                int rowSize4 = customerDao.queryByLevelV4(4, cu4.getId());
                                                                if(total4 >= 9000){
                                                                    if(rowSize4 >= 3){
                                                                        cu4.setLevelCopy4Id(5);
                                                                        customerDao.update(cu4);

                                                                        if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                            Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                            if(cu5 != null) {
                                                                                // 判断王者是否升V5
                                                                                // 查询下级是否有27000单同时拥有3个V4
                                                                                int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                if(total5 >= 27000){
                                                                                    if(rowSize5 >= 3){
                                                                                        cu5.setLevelCopy4Id(6);
                                                                                        customerDao.update(cu5);

                                                                                        if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                            Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                            if(cu6 != null) {
                                                                                                // 判断王者是否升V6
                                                                                                // 查询下级是否有81000单同时拥有3个V5
                                                                                                int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                if(total6 >= 81000){
                                                                                                    if(rowSize6 >= 3){
                                                                                                        cu6.setLevelCopy4Id(7);
                                                                                                        customerDao.update(cu6);

                                                                                                    }else {
                                                                                                        List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                        if (queryList6.size() > 0) {
                                                                                                            int index6 = rowSize6;
                                                                                                            for (Customer c6 : queryList6) {
                                                                                                                int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                if (temp6 > 0) {
                                                                                                                    c6.setLevelV5("1");
                                                                                                                    customerDao.update(c6);
                                                                                                                }
                                                                                                                index6 += temp6;
                                                                                                                if (index6 >= 3) {
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);
                                                                                                                    break;
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }

                                                                                    }else {
                                                                                        List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                        if (queryList5.size() > 0) {
                                                                                            int index5 = rowSize5;
                                                                                            for (Customer c5 : queryList5) {
                                                                                                int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                if (temp5 > 0) {
                                                                                                    c5.setLevelV4("1");
                                                                                                    customerDao.update(c5);
                                                                                                }
                                                                                                index5 += temp5;
                                                                                                if (index5 >= 3) {
                                                                                                    cu5.setLevelCopy4Id(6);
                                                                                                    customerDao.update(cu5);

                                                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                        if(cu6 != null) {
                                                                                                            // 判断王者是否升V6
                                                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                            if(total6 >= 81000){
                                                                                                                if(rowSize6 >= 3){
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);

                                                                                                                }else {
                                                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                    if (queryList6.size() > 0) {
                                                                                                                        int index6 = rowSize6;
                                                                                                                        for (Customer c6 : queryList6) {
                                                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                            if (temp6 > 0) {
                                                                                                                                c6.setLevelV5("1");
                                                                                                                                customerDao.update(c6);
                                                                                                                            }
                                                                                                                            index6 += temp6;
                                                                                                                            if (index6 >= 3) {
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);
                                                                                                                                break;
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                    break;
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }

                                                                    }else {
                                                                        List<Customer> queryList4 = customerDao.queryCustomerByLevelCopy4Id(4, cu4.getId());
                                                                        if (queryList4.size() > 0) {
                                                                            int index4 = rowSize4;
                                                                            for (Customer c4 : queryList4) {
                                                                                int temp4 = customerService.recursionCustomer_4(4, c4.getId());
                                                                                if (temp4 > 0) {
                                                                                    c4.setLevelV3("1");
                                                                                    customerDao.update(c4);
                                                                                }
                                                                                index4 += temp4;
                                                                                if (index4 >= 3) {
                                                                                    cu4.setLevelCopy4Id(5);
                                                                                    customerDao.update(cu4);

                                                                                    if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                                        Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                                        if(cu5 != null) {
                                                                                            // 判断王者是否升V5
                                                                                            // 查询下级是否有27000单同时拥有3个V4
                                                                                            int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                            int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                            if(total5 >= 27000){
                                                                                                if(rowSize5 >= 3){
                                                                                                    cu5.setLevelCopy4Id(6);
                                                                                                    customerDao.update(cu5);

                                                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                        if(cu6 != null) {
                                                                                                            // 判断王者是否升V6
                                                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                            if(total6 >= 81000){
                                                                                                                if(rowSize6 >= 3){
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);

                                                                                                                }else {
                                                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                    if (queryList6.size() > 0) {
                                                                                                                        int index6 = rowSize6;
                                                                                                                        for (Customer c6 : queryList6) {
                                                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                            if (temp6 > 0) {
                                                                                                                                c6.setLevelV5("1");
                                                                                                                                customerDao.update(c6);
                                                                                                                            }
                                                                                                                            index6 += temp6;
                                                                                                                            if (index6 >= 3) {
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);
                                                                                                                                break;
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }

                                                                                                }else {
                                                                                                    List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                                    if (queryList5.size() > 0) {
                                                                                                        int index5 = rowSize5;
                                                                                                        for (Customer c5 : queryList5) {
                                                                                                            int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                            if (temp5 > 0) {
                                                                                                                c5.setLevelV4("1");
                                                                                                                customerDao.update(c5);
                                                                                                            }
                                                                                                            index5 += temp5;
                                                                                                            if (index5 >= 3) {
                                                                                                                cu5.setLevelCopy4Id(6);
                                                                                                                customerDao.update(cu5);

                                                                                                                if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                    Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                    if(cu6 != null) {
                                                                                                                        // 判断王者是否升V6
                                                                                                                        // 查询下级是否有81000单同时拥有3个V5
                                                                                                                        int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                        int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                        if(total6 >= 81000){
                                                                                                                            if(rowSize6 >= 3){
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);

                                                                                                                            }else {
                                                                                                                                List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                if (queryList6.size() > 0) {
                                                                                                                                    int index6 = rowSize6;
                                                                                                                                    for (Customer c6 : queryList6) {
                                                                                                                                        int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                        if (temp6 > 0) {
                                                                                                                                            c6.setLevelV5("1");
                                                                                                                                            customerDao.update(c6);
                                                                                                                                        }
                                                                                                                                        index6 += temp6;
                                                                                                                                        if (index6 >= 3) {
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);
                                                                                                                                            break;
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                                break;
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    break;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }

                                                    }else {
                                                        List<Customer> queryList3 = customerDao.queryCustomerByLevelCopy4Id(3, cu3.getId());
                                                        if (queryList3.size() > 0) {
                                                            int index3 = rowSize3;
                                                            for (Customer c3 : queryList3) {
                                                                int temp3 = customerService.recursionCustomer_4(3, c3.getId());
                                                                if (temp3 > 0) {
                                                                    c3.setLevelV2("1");
                                                                    customerDao.update(c3);
                                                                }
                                                                index3 += temp3;
                                                                if (index3 >= 3) {
                                                                    cu3.setLevelCopy4Id(4);
                                                                    customerDao.update(cu3);

                                                                    if(cu3.getParentId() != null && cu3.getParentId() != 1198 && cu3.getLevelCopy4Id() != 8) {
                                                                        Customer cu4 = customerDao.queryById(cu3.getParentId());
                                                                        if (cu4 != null) {
                                                                            // 判断王者是否升V4
                                                                            // 查询下级是否有9000单同时拥有3个V3
                                                                            int total4 = customerDao.queryByOrderQuantityTotal(cu4.getId());
                                                                            int rowSize4 = customerDao.queryByLevelV4(4, cu4.getId());
                                                                            if(total4 >= 9000){
                                                                                if(rowSize4 >= 3){
                                                                                    cu4.setLevelCopy4Id(5);
                                                                                    customerDao.update(cu4);

                                                                                    if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                                        Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                                        if(cu5 != null) {
                                                                                            // 判断王者是否升V5
                                                                                            // 查询下级是否有27000单同时拥有3个V4
                                                                                            int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                            int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                            if(total5 >= 27000){
                                                                                                if(rowSize5 >= 3){
                                                                                                    cu5.setLevelCopy4Id(6);
                                                                                                    customerDao.update(cu5);

                                                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                        if(cu6 != null) {
                                                                                                            // 判断王者是否升V6
                                                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                            if(total6 >= 81000){
                                                                                                                if(rowSize6 >= 3){
                                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                                    customerDao.update(cu6);

                                                                                                                }else {
                                                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                    if (queryList6.size() > 0) {
                                                                                                                        int index6 = rowSize6;
                                                                                                                        for (Customer c6 : queryList6) {
                                                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                            if (temp6 > 0) {
                                                                                                                                c6.setLevelV5("1");
                                                                                                                                customerDao.update(c6);
                                                                                                                            }
                                                                                                                            index6 += temp6;
                                                                                                                            if (index6 >= 3) {
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);
                                                                                                                                break;
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }

                                                                                                }else {
                                                                                                    List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                                    if (queryList5.size() > 0) {
                                                                                                        int index5 = rowSize5;
                                                                                                        for (Customer c5 : queryList5) {
                                                                                                            int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                            if (temp5 > 0) {
                                                                                                                c5.setLevelV4("1");
                                                                                                                customerDao.update(c5);
                                                                                                            }
                                                                                                            index5 += temp5;
                                                                                                            if (index5 >= 3) {
                                                                                                                cu5.setLevelCopy4Id(6);
                                                                                                                customerDao.update(cu5);

                                                                                                                if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                    Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                    if(cu6 != null) {
                                                                                                                        // 判断王者是否升V6
                                                                                                                        // 查询下级是否有81000单同时拥有3个V5
                                                                                                                        int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                        int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                        if(total6 >= 81000){
                                                                                                                            if(rowSize6 >= 3){
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);

                                                                                                                            }else {
                                                                                                                                List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                if (queryList6.size() > 0) {
                                                                                                                                    int index6 = rowSize6;
                                                                                                                                    for (Customer c6 : queryList6) {
                                                                                                                                        int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                        if (temp6 > 0) {
                                                                                                                                            c6.setLevelV5("1");
                                                                                                                                            customerDao.update(c6);
                                                                                                                                        }
                                                                                                                                        index6 += temp6;
                                                                                                                                        if (index6 >= 3) {
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);
                                                                                                                                            break;
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                                break;
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }

                                                                                }else {
                                                                                    List<Customer> queryList4 = customerDao.queryCustomerByLevelCopy4Id(4, cu4.getId());
                                                                                    if (queryList4.size() > 0) {
                                                                                        int index4 = rowSize4;
                                                                                        for (Customer c4 : queryList4) {
                                                                                            int temp4 = customerService.recursionCustomer_4(4, c4.getId());
                                                                                            if (temp4 > 0) {
                                                                                                c4.setLevelV3("1");
                                                                                                customerDao.update(c4);
                                                                                            }
                                                                                            index4 += temp4;
                                                                                            if (index4 >= 3) {
                                                                                                cu4.setLevelCopy4Id(5);
                                                                                                customerDao.update(cu4);

                                                                                                if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                                                    Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                                                    if(cu5 != null) {
                                                                                                        // 判断王者是否升V5
                                                                                                        // 查询下级是否有27000单同时拥有3个V4
                                                                                                        int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                                                        int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                                                        if(total5 >= 27000){
                                                                                                            if(rowSize5 >= 3){
                                                                                                                cu5.setLevelCopy4Id(6);
                                                                                                                customerDao.update(cu5);

                                                                                                                if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                    Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                    if(cu6 != null) {
                                                                                                                        // 判断王者是否升V6
                                                                                                                        // 查询下级是否有81000单同时拥有3个V5
                                                                                                                        int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                        int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                        if(total6 >= 81000){
                                                                                                                            if(rowSize6 >= 3){
                                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                                customerDao.update(cu6);

                                                                                                                            }else {
                                                                                                                                List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                if (queryList6.size() > 0) {
                                                                                                                                    int index6 = rowSize6;
                                                                                                                                    for (Customer c6 : queryList6) {
                                                                                                                                        int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                        if (temp6 > 0) {
                                                                                                                                            c6.setLevelV5("1");
                                                                                                                                            customerDao.update(c6);
                                                                                                                                        }
                                                                                                                                        index6 += temp6;
                                                                                                                                        if (index6 >= 3) {
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);
                                                                                                                                            break;
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }

                                                                                                            }else {
                                                                                                                List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                                                if (queryList5.size() > 0) {
                                                                                                                    int index5 = rowSize5;
                                                                                                                    for (Customer c5 : queryList5) {
                                                                                                                        int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                                                        if (temp5 > 0) {
                                                                                                                            c5.setLevelV4("1");
                                                                                                                            customerDao.update(c5);
                                                                                                                        }
                                                                                                                        index5 += temp5;
                                                                                                                        if (index5 >= 3) {
                                                                                                                            cu5.setLevelCopy4Id(6);
                                                                                                                            customerDao.update(cu5);

                                                                                                                            if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                                                Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                                                if(cu6 != null) {
                                                                                                                                    // 判断王者是否升V6
                                                                                                                                    // 查询下级是否有81000单同时拥有3个V5
                                                                                                                                    int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                                                    int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                                                    if(total6 >= 81000){
                                                                                                                                        if(rowSize6 >= 3){
                                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                                            customerDao.update(cu6);

                                                                                                                                        }else {
                                                                                                                                            List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                                            if (queryList6.size() > 0) {
                                                                                                                                                int index6 = rowSize6;
                                                                                                                                                for (Customer c6 : queryList6) {
                                                                                                                                                    int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                                                    if (temp6 > 0) {
                                                                                                                                                        c6.setLevelV5("1");
                                                                                                                                                        customerDao.update(c6);
                                                                                                                                                    }
                                                                                                                                                    index6 += temp6;
                                                                                                                                                    if (index6 >= 3) {
                                                                                                                                                        cu6.setLevelCopy4Id(7);
                                                                                                                                                        customerDao.update(cu6);
                                                                                                                                                        break;
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                            break;
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                                break;
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }

                }else if(cu1.getLevelCopy4Id() == 3){ // v2
                    // 判断是否升V3
                    // 查询下级是否有3000单同时拥有3个V2
                    int total3 = customerDao.queryByOrderQuantityTotal(cu1.getId());
                    int rowSize3 = customerDao.queryByLevelV3(3, cu1.getId());
                    if(total3 >= 3000){
                        if(rowSize3 >= 3){
                            cu1.setLevelCopy4Id(4);
                            customerDao.update(cu1);

                            if(cu1.getParentId() != null && cu1.getParentId() != 1198 && cu1.getLevelCopy4Id() != 8) {
                                Customer cu4 = customerDao.queryById(cu1.getParentId());
                                if (cu4 != null) {
                                    // 判断王者是否升V4
                                    // 查询下级是否有9000单同时拥有3个V3
                                    int total4 = customerDao.queryByOrderQuantityTotal(cu4.getId());
                                    int rowSize4 = customerDao.queryByLevelV4(4, cu4.getId());
                                    if(total4 >= 9000){
                                        if(rowSize4 >= 3){
                                            cu4.setLevelCopy4Id(5);
                                            customerDao.update(cu4);

                                            if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                if(cu5 != null) {
                                                    // 判断王者是否升V5
                                                    // 查询下级是否有27000单同时拥有3个V4
                                                    int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                    int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                    if(total5 >= 27000){
                                                        if(rowSize5 >= 3){
                                                            cu5.setLevelCopy4Id(6);
                                                            customerDao.update(cu5);

                                                            if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                if(cu6 != null) {
                                                                    // 判断王者是否升V6
                                                                    // 查询下级是否有81000单同时拥有3个V5
                                                                    int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                    int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                    if(total6 >= 81000){
                                                                        if(rowSize6 >= 3){
                                                                            cu6.setLevelCopy4Id(7);
                                                                            customerDao.update(cu6);

                                                                        }else {
                                                                            List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                            if (queryList6.size() > 0) {
                                                                                int index6 = rowSize6;
                                                                                for (Customer c6 : queryList6) {
                                                                                    int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                    if (temp6 > 0) {
                                                                                        c6.setLevelV5("1");
                                                                                        customerDao.update(c6);
                                                                                    }
                                                                                    index6 += temp6;
                                                                                    if (index6 >= 3) {
                                                                                        cu6.setLevelCopy4Id(7);
                                                                                        customerDao.update(cu6);
                                                                                        break;
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }

                                                        }else {
                                                            List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                            if (queryList5.size() > 0) {
                                                                int index5 = rowSize5;
                                                                for (Customer c5 : queryList5) {
                                                                    int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                    if (temp5 > 0) {
                                                                        c5.setLevelV4("1");
                                                                        customerDao.update(c5);
                                                                    }
                                                                    index5 += temp5;
                                                                    if (index5 >= 3) {
                                                                        cu5.setLevelCopy4Id(6);
                                                                        customerDao.update(cu5);

                                                                        if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                            Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                            if(cu6 != null) {
                                                                                // 判断王者是否升V6
                                                                                // 查询下级是否有81000单同时拥有3个V5
                                                                                int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                if(total6 >= 81000){
                                                                                    if(rowSize6 >= 3){
                                                                                        cu6.setLevelCopy4Id(7);
                                                                                        customerDao.update(cu6);

                                                                                    }else {
                                                                                        List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                        if (queryList6.size() > 0) {
                                                                                            int index6 = rowSize6;
                                                                                            for (Customer c6 : queryList6) {
                                                                                                int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                if (temp6 > 0) {
                                                                                                    c6.setLevelV5("1");
                                                                                                    customerDao.update(c6);
                                                                                                }
                                                                                                index6 += temp6;
                                                                                                if (index6 >= 3) {
                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                    customerDao.update(cu6);
                                                                                                    break;
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }

                                        }else {
                                            List<Customer> queryList4 = customerDao.queryCustomerByLevelCopy4Id(4, cu4.getId());
                                            if (queryList4.size() > 0) {
                                                int index4 = rowSize4;
                                                for (Customer c4 : queryList4) {
                                                    int temp4 = customerService.recursionCustomer_4(4, c4.getId());
                                                    if (temp4 > 0) {
                                                        c4.setLevelV3("1");
                                                        customerDao.update(c4);
                                                    }
                                                    index4 += temp4;
                                                    if (index4 >= 3) {
                                                        cu4.setLevelCopy4Id(5);
                                                        customerDao.update(cu4);

                                                        if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                            Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                            if(cu5 != null) {
                                                                // 判断王者是否升V5
                                                                // 查询下级是否有27000单同时拥有3个V4
                                                                int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                if(total5 >= 27000){
                                                                    if(rowSize5 >= 3){
                                                                        cu5.setLevelCopy4Id(6);
                                                                        customerDao.update(cu5);

                                                                        if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                            Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                            if(cu6 != null) {
                                                                                // 判断王者是否升V6
                                                                                // 查询下级是否有81000单同时拥有3个V5
                                                                                int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                if(total6 >= 81000){
                                                                                    if(rowSize6 >= 3){
                                                                                        cu6.setLevelCopy4Id(7);
                                                                                        customerDao.update(cu6);

                                                                                    }else {
                                                                                        List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                        if (queryList6.size() > 0) {
                                                                                            int index6 = rowSize6;
                                                                                            for (Customer c6 : queryList6) {
                                                                                                int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                if (temp6 > 0) {
                                                                                                    c6.setLevelV5("1");
                                                                                                    customerDao.update(c6);
                                                                                                }
                                                                                                index6 += temp6;
                                                                                                if (index6 >= 3) {
                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                    customerDao.update(cu6);
                                                                                                    break;
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }

                                                                    }else {
                                                                        List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                        if (queryList5.size() > 0) {
                                                                            int index5 = rowSize5;
                                                                            for (Customer c5 : queryList5) {
                                                                                int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                if (temp5 > 0) {
                                                                                    c5.setLevelV4("1");
                                                                                    customerDao.update(c5);
                                                                                }
                                                                                index5 += temp5;
                                                                                if (index5 >= 3) {
                                                                                    cu5.setLevelCopy4Id(6);
                                                                                    customerDao.update(cu5);

                                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                        if(cu6 != null) {
                                                                                            // 判断王者是否升V6
                                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                            if(total6 >= 81000){
                                                                                                if(rowSize6 >= 3){
                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                    customerDao.update(cu6);

                                                                                                }else {
                                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                    if (queryList6.size() > 0) {
                                                                                                        int index6 = rowSize6;
                                                                                                        for (Customer c6 : queryList6) {
                                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                            if (temp6 > 0) {
                                                                                                                c6.setLevelV5("1");
                                                                                                                customerDao.update(c6);
                                                                                                            }
                                                                                                            index6 += temp6;
                                                                                                            if (index6 >= 3) {
                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                customerDao.update(cu6);
                                                                                                                break;
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    break;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }else {
                            List<Customer> queryList3 = customerDao.queryCustomerByLevelCopy4Id(3, cu1.getId());
                            if (queryList3.size() > 0) {
                                int index3 = rowSize3;
                                for (Customer c3 : queryList3) {
                                    int temp3 = customerService.recursionCustomer_4(3, c3.getId());
                                    if (temp3 > 0) {
                                        c3.setLevelV2("1");
                                        customerDao.update(c3);
                                    }
                                    index3 += temp3;
                                    if (index3 >= 3) {
                                        cu1.setLevelCopy4Id(4);
                                        customerDao.update(cu1);

                                        if(cu1.getParentId() != null && cu1.getParentId() != 1198 && cu1.getLevelCopy4Id() != 8) {
                                            Customer cu4 = customerDao.queryById(cu1.getParentId());
                                            if (cu4 != null) {
                                                // 判断王者是否升V4
                                                // 查询下级是否有9000单同时拥有3个V3
                                                int total4 = customerDao.queryByOrderQuantityTotal(cu4.getId());
                                                int rowSize4 = customerDao.queryByLevelV4(4, cu4.getId());
                                                if(total4 >= 9000){
                                                    if(rowSize4 >= 3){
                                                        cu4.setLevelCopy4Id(5);
                                                        customerDao.update(cu4);

                                                        if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                            Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                            if(cu5 != null) {
                                                                // 判断王者是否升V5
                                                                // 查询下级是否有27000单同时拥有3个V4
                                                                int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                if(total5 >= 27000){
                                                                    if(rowSize5 >= 3){
                                                                        cu5.setLevelCopy4Id(6);
                                                                        customerDao.update(cu5);

                                                                        if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                            Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                            if(cu6 != null) {
                                                                                // 判断王者是否升V6
                                                                                // 查询下级是否有81000单同时拥有3个V5
                                                                                int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                if(total6 >= 81000){
                                                                                    if(rowSize6 >= 3){
                                                                                        cu6.setLevelCopy4Id(7);
                                                                                        customerDao.update(cu6);

                                                                                    }else {
                                                                                        List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                        if (queryList6.size() > 0) {
                                                                                            int index6 = rowSize6;
                                                                                            for (Customer c6 : queryList6) {
                                                                                                int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                if (temp6 > 0) {
                                                                                                    c6.setLevelV5("1");
                                                                                                    customerDao.update(c6);
                                                                                                }
                                                                                                index6 += temp6;
                                                                                                if (index6 >= 3) {
                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                    customerDao.update(cu6);
                                                                                                    break;
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }

                                                                    }else {
                                                                        List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                        if (queryList5.size() > 0) {
                                                                            int index5 = rowSize5;
                                                                            for (Customer c5 : queryList5) {
                                                                                int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                if (temp5 > 0) {
                                                                                    c5.setLevelV4("1");
                                                                                    customerDao.update(c5);
                                                                                }
                                                                                index5 += temp5;
                                                                                if (index5 >= 3) {
                                                                                    cu5.setLevelCopy4Id(6);
                                                                                    customerDao.update(cu5);

                                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                        if(cu6 != null) {
                                                                                            // 判断王者是否升V6
                                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                            if(total6 >= 81000){
                                                                                                if(rowSize6 >= 3){
                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                    customerDao.update(cu6);

                                                                                                }else {
                                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                    if (queryList6.size() > 0) {
                                                                                                        int index6 = rowSize6;
                                                                                                        for (Customer c6 : queryList6) {
                                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                            if (temp6 > 0) {
                                                                                                                c6.setLevelV5("1");
                                                                                                                customerDao.update(c6);
                                                                                                            }
                                                                                                            index6 += temp6;
                                                                                                            if (index6 >= 3) {
                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                customerDao.update(cu6);
                                                                                                                break;
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    break;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }

                                                    }else {
                                                        List<Customer> queryList4 = customerDao.queryCustomerByLevelCopy4Id(4, cu4.getId());
                                                        if (queryList4.size() > 0) {
                                                            int index4 = rowSize4;
                                                            for (Customer c4 : queryList4) {
                                                                int temp4 = customerService.recursionCustomer_4(4, c4.getId());
                                                                if (temp4 > 0) {
                                                                    c4.setLevelV3("1");
                                                                    customerDao.update(c4);
                                                                }
                                                                index4 += temp4;
                                                                if (index4 >= 3) {
                                                                    cu4.setLevelCopy4Id(5);
                                                                    customerDao.update(cu4);

                                                                    if(cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopy4Id() != 8){
                                                                        Customer cu5 = customerDao.queryById(cu4.getParentId());
                                                                        if(cu5 != null) {
                                                                            // 判断王者是否升V5
                                                                            // 查询下级是否有27000单同时拥有3个V4
                                                                            int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                                            int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                                            if(total5 >= 27000){
                                                                                if(rowSize5 >= 3){
                                                                                    cu5.setLevelCopy4Id(6);
                                                                                    customerDao.update(cu5);

                                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                        if(cu6 != null) {
                                                                                            // 判断王者是否升V6
                                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                            if(total6 >= 81000){
                                                                                                if(rowSize6 >= 3){
                                                                                                    cu6.setLevelCopy4Id(7);
                                                                                                    customerDao.update(cu6);

                                                                                                }else {
                                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                    if (queryList6.size() > 0) {
                                                                                                        int index6 = rowSize6;
                                                                                                        for (Customer c6 : queryList6) {
                                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                            if (temp6 > 0) {
                                                                                                                c6.setLevelV5("1");
                                                                                                                customerDao.update(c6);
                                                                                                            }
                                                                                                            index6 += temp6;
                                                                                                            if (index6 >= 3) {
                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                customerDao.update(cu6);
                                                                                                                break;
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }

                                                                                }else {
                                                                                    List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                                                    if (queryList5.size() > 0) {
                                                                                        int index5 = rowSize5;
                                                                                        for (Customer c5 : queryList5) {
                                                                                            int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                                            if (temp5 > 0) {
                                                                                                c5.setLevelV4("1");
                                                                                                customerDao.update(c5);
                                                                                            }
                                                                                            index5 += temp5;
                                                                                            if (index5 >= 3) {
                                                                                                cu5.setLevelCopy4Id(6);
                                                                                                customerDao.update(cu5);

                                                                                                if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                                                    Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                                                    if(cu6 != null) {
                                                                                                        // 判断王者是否升V6
                                                                                                        // 查询下级是否有81000单同时拥有3个V5
                                                                                                        int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                                                        int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                                                        if(total6 >= 81000){
                                                                                                            if(rowSize6 >= 3){
                                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                                customerDao.update(cu6);

                                                                                                            }else {
                                                                                                                List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                                                if (queryList6.size() > 0) {
                                                                                                                    int index6 = rowSize6;
                                                                                                                    for (Customer c6 : queryList6) {
                                                                                                                        int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                                                        if (temp6 > 0) {
                                                                                                                            c6.setLevelV5("1");
                                                                                                                            customerDao.update(c6);
                                                                                                                        }
                                                                                                                        index6 += temp6;
                                                                                                                        if (index6 >= 3) {
                                                                                                                            cu6.setLevelCopy4Id(7);
                                                                                                                            customerDao.update(cu6);
                                                                                                                            break;
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                                break;
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }

                }else if(cu1.getLevelCopy4Id() == 4){ // v3
                    // 判断是否升V4
                    // 查询下级是否有9000单同时拥有3个V3
                    int total4 = customerDao.queryByOrderQuantityTotal(cu1.getId());
                    int rowSize4 = customerDao.queryByLevelV4(4, cu1.getId());
                    if(total4 >= 9000){
                        if(rowSize4 >= 3){
                            cu1.setLevelCopy4Id(5);
                            customerDao.update(cu1);

                            if(cu1.getParentId() != null && cu1.getParentId() != 1198 && cu1.getLevelCopy4Id() != 8){
                                Customer cu5 = customerDao.queryById(cu1.getParentId());
                                if(cu5 != null) {
                                    // 判断王者是否升V5
                                    // 查询下级是否有27000单同时拥有3个V4
                                    int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                    int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                    if(total5 >= 27000){
                                        if(rowSize5 >= 3){
                                            cu5.setLevelCopy4Id(6);
                                            customerDao.update(cu5);

                                            if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                if(cu6 != null) {
                                                    // 判断王者是否升V6
                                                    // 查询下级是否有81000单同时拥有3个V5
                                                    int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                    int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                    if(total6 >= 81000){
                                                        if(rowSize6 >= 3){
                                                            cu6.setLevelCopy4Id(7);
                                                            customerDao.update(cu6);

                                                        }else {
                                                            List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                            if (queryList6.size() > 0) {
                                                                int index6 = rowSize6;
                                                                for (Customer c6 : queryList6) {
                                                                    int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                    if (temp6 > 0) {
                                                                        c6.setLevelV5("1");
                                                                        customerDao.update(c6);
                                                                    }
                                                                    index6 += temp6;
                                                                    if (index6 >= 3) {
                                                                        cu6.setLevelCopy4Id(7);
                                                                        customerDao.update(cu6);
                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }

                                        }else {
                                            List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                            if (queryList5.size() > 0) {
                                                int index5 = rowSize5;
                                                for (Customer c5 : queryList5) {
                                                    int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                    if (temp5 > 0) {
                                                        c5.setLevelV4("1");
                                                        customerDao.update(c5);
                                                    }
                                                    index5 += temp5;
                                                    if (index5 >= 3) {
                                                        cu5.setLevelCopy4Id(6);
                                                        customerDao.update(cu5);

                                                        if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                            Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                            if(cu6 != null) {
                                                                // 判断王者是否升V6
                                                                // 查询下级是否有81000单同时拥有3个V5
                                                                int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                if(total6 >= 81000){
                                                                    if(rowSize6 >= 3){
                                                                        cu6.setLevelCopy4Id(7);
                                                                        customerDao.update(cu6);

                                                                    }else {
                                                                        List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                        if (queryList6.size() > 0) {
                                                                            int index6 = rowSize6;
                                                                            for (Customer c6 : queryList6) {
                                                                                int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                if (temp6 > 0) {
                                                                                    c6.setLevelV5("1");
                                                                                    customerDao.update(c6);
                                                                                }
                                                                                index6 += temp6;
                                                                                if (index6 >= 3) {
                                                                                    cu6.setLevelCopy4Id(7);
                                                                                    customerDao.update(cu6);
                                                                                    break;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }else {
                            List<Customer> queryList4 = customerDao.queryCustomerByLevelCopy4Id(4, cu1.getId());
                            if (queryList4.size() > 0) {
                                int index4 = rowSize4;
                                for (Customer c4 : queryList4) {
                                    int temp4 = customerService.recursionCustomer_4(4, c4.getId());
                                    if (temp4 > 0) {
                                        c4.setLevelV3("1");
                                        customerDao.update(c4);
                                    }
                                    index4 += temp4;
                                    if (index4 >= 3) {
                                        cu1.setLevelCopy4Id(5);
                                        customerDao.update(cu1);

                                        if(cu1.getParentId() != null && cu1.getParentId() != 1198 && cu1.getLevelCopy4Id() != 8){
                                            Customer cu5 = customerDao.queryById(cu1.getParentId());
                                            if(cu5 != null) {
                                                // 判断王者是否升V5
                                                // 查询下级是否有27000单同时拥有3个V4
                                                int total5 = customerDao.queryByOrderQuantityTotal(cu5.getId());
                                                int rowSize5 = customerDao.queryByLevelV5(5, cu5.getId());
                                                if(total5 >= 27000){
                                                    if(rowSize5 >= 3){
                                                        cu5.setLevelCopy4Id(6);
                                                        customerDao.update(cu5);

                                                        if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                            Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                            if(cu6 != null) {
                                                                // 判断王者是否升V6
                                                                // 查询下级是否有81000单同时拥有3个V5
                                                                int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                if(total6 >= 81000){
                                                                    if(rowSize6 >= 3){
                                                                        cu6.setLevelCopy4Id(7);
                                                                        customerDao.update(cu6);

                                                                    }else {
                                                                        List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                        if (queryList6.size() > 0) {
                                                                            int index6 = rowSize6;
                                                                            for (Customer c6 : queryList6) {
                                                                                int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                if (temp6 > 0) {
                                                                                    c6.setLevelV5("1");
                                                                                    customerDao.update(c6);
                                                                                }
                                                                                index6 += temp6;
                                                                                if (index6 >= 3) {
                                                                                    cu6.setLevelCopy4Id(7);
                                                                                    customerDao.update(cu6);
                                                                                    break;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }

                                                    }else {
                                                        List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu5.getId());
                                                        if (queryList5.size() > 0) {
                                                            int index5 = rowSize5;
                                                            for (Customer c5 : queryList5) {
                                                                int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                                                if (temp5 > 0) {
                                                                    c5.setLevelV4("1");
                                                                    customerDao.update(c5);
                                                                }
                                                                index5 += temp5;
                                                                if (index5 >= 3) {
                                                                    cu5.setLevelCopy4Id(6);
                                                                    customerDao.update(cu5);

                                                                    if(cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopy4Id() != 8){
                                                                        Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                                        if(cu6 != null) {
                                                                            // 判断王者是否升V6
                                                                            // 查询下级是否有81000单同时拥有3个V5
                                                                            int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                                            int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                                            if(total6 >= 81000){
                                                                                if(rowSize6 >= 3){
                                                                                    cu6.setLevelCopy4Id(7);
                                                                                    customerDao.update(cu6);

                                                                                }else {
                                                                                    List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                                                    if (queryList6.size() > 0) {
                                                                                        int index6 = rowSize6;
                                                                                        for (Customer c6 : queryList6) {
                                                                                            int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                                            if (temp6 > 0) {
                                                                                                c6.setLevelV5("1");
                                                                                                customerDao.update(c6);
                                                                                            }
                                                                                            index6 += temp6;
                                                                                            if (index6 >= 3) {
                                                                                                cu6.setLevelCopy4Id(7);
                                                                                                customerDao.update(cu6);
                                                                                                break;
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }

                }else if(cu1.getLevelCopy4Id() == 5){ // v4
                    // 判断是否升V5
                    // 查询下级是否有27000单同时拥有3个V4
                    int total5 = customerDao.queryByOrderQuantityTotal(cu1.getId());
                    int rowSize5 = customerDao.queryByLevelV5(5, cu1.getId());
                    if(total5 >= 27000){
                        if(rowSize5 >= 3){
                            cu1.setLevelCopy4Id(6);
                            customerDao.update(cu1);

                            if(cu1.getParentId() != null && cu1.getParentId() != 1198 && cu1.getLevelCopy4Id() != 8){
                                Customer cu6 = customerDao.queryById(cu1.getParentId());
                                if(cu6 != null) {
                                    // 判断王者是否升V6
                                    // 查询下级是否有81000单同时拥有3个V5
                                    int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                    int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                    if(total6 >= 81000){
                                        if(rowSize6 >= 3){
                                            cu6.setLevelCopy4Id(7);
                                            customerDao.update(cu6);

                                        }else {
                                            List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                            if (queryList6.size() > 0) {
                                                int index6 = rowSize6;
                                                for (Customer c6 : queryList6) {
                                                    int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                    if (temp6 > 0) {
                                                        c6.setLevelV5("1");
                                                        customerDao.update(c6);
                                                    }
                                                    index6 += temp6;
                                                    if (index6 >= 3) {
                                                        cu6.setLevelCopy4Id(7);
                                                        customerDao.update(cu6);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }else {
                            List<Customer> queryList5 = customerDao.queryCustomerByLevelCopy4Id(5, cu1.getId());
                            if (queryList5.size() > 0) {
                                int index5 = rowSize5;
                                for (Customer c5 : queryList5) {
                                    int temp5 = customerService.recursionCustomer_4(5, c5.getId());
                                    if (temp5 > 0) {
                                        c5.setLevelV4("1");
                                        customerDao.update(c5);
                                    }
                                    index5 += temp5;
                                    if (index5 >= 3) {
                                        cu1.setLevelCopy4Id(6);
                                        customerDao.update(cu1);

                                        if(cu1.getParentId() != null && cu1.getParentId() != 1198 && cu1.getLevelCopy4Id() != 8){
                                            Customer cu6 = customerDao.queryById(cu1.getParentId());
                                            if(cu6 != null) {
                                                // 判断王者是否升V6
                                                // 查询下级是否有81000单同时拥有3个V5
                                                int total6 = customerDao.queryByOrderQuantityTotal(cu6.getId());
                                                int rowSize6 = customerDao.queryByLevelV5(6, cu6.getId());
                                                if(total6 >= 81000){
                                                    if(rowSize6 >= 3){
                                                        cu6.setLevelCopy4Id(7);
                                                        customerDao.update(cu6);

                                                    }else {
                                                        List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu6.getId());
                                                        if (queryList6.size() > 0) {
                                                            int index6 = rowSize6;
                                                            for (Customer c6 : queryList6) {
                                                                int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                                                if (temp6 > 0) {
                                                                    c6.setLevelV5("1");
                                                                    customerDao.update(c6);
                                                                }
                                                                index6 += temp6;
                                                                if (index6 >= 3) {
                                                                    cu6.setLevelCopy4Id(7);
                                                                    customerDao.update(cu6);
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }

                }else if(cu1.getLevelCopy4Id() == 6){ // v5
                    // 判断是否升V6
                    // 查询下级是否有81000单同时拥有3个V5
                    int total6 = customerDao.queryByOrderQuantityTotal(cu1.getId());
                    int rowSize6 = customerDao.queryByLevelV5(6, cu1.getId());
                    if(total6 >= 81000){
                        if(rowSize6 >= 3){
                            cu1.setLevelCopy4Id(7);
                            customerDao.update(cu1);

                        }else {
                            List<Customer> queryList6 = customerDao.queryCustomerByLevelCopy4Id(6, cu1.getId());
                            if (queryList6.size() > 0) {
                                int index6 = rowSize6;
                                for (Customer c6 : queryList6) {
                                    int temp6 = customerService.recursionCustomer_4(6, c6.getId());
                                    if (temp6 > 0) {
                                        c6.setLevelV5("1");
                                        customerDao.update(c6);
                                    }
                                    index6 += temp6;
                                    if (index6 >= 3) {
                                        cu1.setLevelCopy4Id(7);
                                        customerDao.update(cu1);
                                        break;
                                    }
                                }
                            }
                        }
                    }

                }else if(cu1.getLevelCopy4Id() == 7){
                    // 不升级了

                }
            }
        }
    }


    /**
     * 共享模式 处理用户购买后添加业绩和单数
     * @param orders
     */
    public void addAchievementAndOrderQuantity(Orders orders){
        BigDecimal totalAmount = orders.getPayAmount(); // 实际支付的金额
        Integer orderQuantity = 0;
        if(totalAmount.doubleValue() >= 369 && totalAmount.doubleValue() < 3690){
            orderQuantity = 1;
        }else if(totalAmount.doubleValue() >= 3690 && totalAmount.doubleValue() < 36900){
            orderQuantity = 10;
        }else if(totalAmount.doubleValue() >= 36900){
            orderQuantity = 100;
        }

        // 添加业绩
        Customer customer = customerDao.queryById(orders.getOrderPersonId());
        if (customer != null) {
            customer.setAchievement3(customer.getAchievement3().add(totalAmount)); // 团队业绩
            customer.setOrderQuantity(customer.getOrderQuantity() + orderQuantity); // 团队单数
            customer.setOwnTotalBuy3(customer.getOwnTotalBuy3().add(totalAmount)); // 自己购买
            customerDao.update(customer);
            handleAccount2(customer.getId(), totalAmount, customer.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
            if (customer.getParentId() != null && customer.getParentId() != 1198 && customer.getLevelCopyId() != 11) {

                Customer cu1 = customerDao.queryById(customer.getParentId()); // 一层
                if (cu1 != null) {
                    cu1.setAchievement3(cu1.getAchievement3().add(totalAmount)); // 团队业绩
                    cu1.setOrderQuantity(cu1.getOrderQuantity() + orderQuantity); // 团队单数
                    customerDao.update(cu1);
                    handleAccount2(cu1.getId(), totalAmount, cu1.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                    if (cu1.getParentId() != null && cu1.getParentId() != 1198 && cu1.getLevelCopyId() != 11) {

                        Customer cu2 = customerDao.queryById(cu1.getParentId()); // 二层
                        if (cu2 != null) {
                            cu2.setAchievement3(cu2.getAchievement3().add(totalAmount));
                            cu2.setOrderQuantity(cu2.getOrderQuantity() + orderQuantity);
                            customerDao.update(cu2);
                            handleAccount2(cu2.getId(), totalAmount, cu2.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                            if (cu2.getParentId() != null && cu2.getParentId() != 1198 && cu2.getLevelCopyId() != 11) {

                                Customer cu3 = customerDao.queryById(cu2.getParentId()); // 三层
                                if (cu3 != null) {
                                    cu3.setAchievement3(cu3.getAchievement3().add(totalAmount));
                                    cu3.setOrderQuantity(cu3.getOrderQuantity() + orderQuantity);
                                    customerDao.update(cu3);
                                    handleAccount2(cu3.getId(), totalAmount, cu3.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                    if (cu3.getParentId() != null && cu3.getParentId() != 1198 && cu3.getLevelCopyId() != 11) {

                                        Customer cu4 = customerDao.queryById(cu3.getParentId()); // 四层
                                        if (cu4 != null) {
                                            cu4.setAchievement3(cu4.getAchievement3().add(totalAmount));
                                            cu4.setOrderQuantity(cu4.getOrderQuantity() + orderQuantity);
                                            customerDao.update(cu4);
                                            handleAccount2(cu4.getId(), totalAmount, cu4.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                            if (cu4.getParentId() != null && cu4.getParentId() != 1198 && cu4.getLevelCopyId() != 11) {

                                                Customer cu5 = customerDao.queryById(cu4.getParentId()); // 五层
                                                if (cu5 != null) {
                                                    cu5.setAchievement3(cu5.getAchievement3().add(totalAmount));
                                                    cu5.setOrderQuantity(cu5.getOrderQuantity() + orderQuantity);
                                                    customerDao.update(cu5);
                                                    handleAccount2(cu5.getId(), totalAmount, cu5.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                    if (cu5.getParentId() != null && cu5.getParentId() != 1198 && cu5.getLevelCopyId() != 11) {

                                                        Customer cu6 = customerDao.queryById(cu5.getParentId()); // 六层
                                                        if (cu6 != null) {
                                                            cu6.setAchievement3(cu6.getAchievement3().add(totalAmount));
                                                            cu6.setOrderQuantity(cu6.getOrderQuantity() + orderQuantity);
                                                            customerDao.update(cu6);
                                                            handleAccount2(cu6.getId(), totalAmount, cu6.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                            if (cu6.getParentId() != null && cu6.getParentId() != 1198 && cu6.getLevelCopyId() != 11) {

                                                                Customer cu7 = customerDao.queryById(cu6.getParentId()); // 七层
                                                                if (cu7 != null) {
                                                                    cu7.setAchievement3(cu7.getAchievement3().add(totalAmount));
                                                                    cu7.setOrderQuantity(cu7.getOrderQuantity() + orderQuantity);
                                                                    customerDao.update(cu7);
                                                                    handleAccount2(cu7.getId(), totalAmount, cu7.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                    if (cu7.getParentId() != null && cu7.getParentId() != 1198 && cu7.getLevelCopyId() != 11) {

                                                                        Customer cu8 = customerDao.queryById(cu7.getParentId()); // 八层
                                                                        if (cu8 != null) {
                                                                            cu8.setAchievement3(cu8.getAchievement3().add(totalAmount));
                                                                            cu8.setOrderQuantity(cu8.getOrderQuantity() + orderQuantity);
                                                                            customerDao.update(cu8);
                                                                            handleAccount2(cu8.getId(), totalAmount, cu8.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                            if (cu8.getParentId() != null && cu8.getParentId() != 1198 && cu8.getLevelCopyId() != 11) {

                                                                                Customer cu9 = customerDao.queryById(cu8.getParentId()); // 九层
                                                                                if (cu9 != null) {
                                                                                    cu9.setAchievement3(cu9.getAchievement3().add(totalAmount));
                                                                                    cu9.setOrderQuantity(cu9.getOrderQuantity() + orderQuantity);
                                                                                    customerDao.update(cu9);
                                                                                    handleAccount2(cu9.getId(), totalAmount, cu9.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                    if (cu9.getParentId() != null && cu9.getParentId() != 1198 && cu9.getLevelCopyId() != 11) {

                                                                                        Customer cu10 = customerDao.queryById(cu9.getParentId()); // 十层
                                                                                        if (cu10 != null) {
                                                                                            cu10.setAchievement3(cu10.getAchievement3().add(totalAmount));
                                                                                            cu10.setOrderQuantity(cu10.getOrderQuantity() + orderQuantity);
                                                                                            customerDao.update(cu10);
                                                                                            handleAccount2(cu10.getId(), totalAmount, cu10.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                            if (cu10.getParentId() != null && cu10.getParentId() != 1198 && cu10.getLevelCopyId() != 11) {

                                                                                                Customer cu11 = customerDao.queryById(cu10.getParentId()); // 十一层
                                                                                                if (cu11 != null) {
                                                                                                    cu11.setAchievement3(cu11.getAchievement3().add(totalAmount));
                                                                                                    cu11.setOrderQuantity(cu11.getOrderQuantity() + orderQuantity);
                                                                                                    customerDao.update(cu11);
                                                                                                    handleAccount2(cu11.getId(), totalAmount, cu11.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                    if (cu11.getParentId() != null && cu11.getParentId() != 1198 && cu11.getLevelCopyId() != 11) {

                                                                                                        Customer cu12 = customerDao.queryById(cu11.getParentId()); // 十二层
                                                                                                        if (cu12 != null) {
                                                                                                            cu12.setAchievement3(cu12.getAchievement3().add(totalAmount));
                                                                                                            cu12.setOrderQuantity(cu12.getOrderQuantity() + orderQuantity);
                                                                                                            customerDao.update(cu12);
                                                                                                            handleAccount2(cu12.getId(), totalAmount, cu12.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                            if (cu12.getParentId() != null && cu12.getParentId() != 1198 && cu12.getLevelCopyId() != 11) {

                                                                                                                Customer cu13 = customerDao.queryById(cu12.getParentId()); // 十三层
                                                                                                                if (cu13 != null) {
                                                                                                                    cu13.setAchievement3(cu13.getAchievement3().add(totalAmount));
                                                                                                                    cu13.setOrderQuantity(cu13.getOrderQuantity() + orderQuantity);
                                                                                                                    customerDao.update(cu13);
                                                                                                                    handleAccount2(cu13.getId(), totalAmount, cu13.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                                    if (cu13.getParentId() != null && cu13.getParentId() != 1198 && cu13.getLevelCopyId() != 11) {

                                                                                                                        Customer cu14 = customerDao.queryById(cu13.getParentId()); // 十四层
                                                                                                                        if (cu14 != null) {
                                                                                                                            cu14.setAchievement3(cu14.getAchievement3().add(totalAmount));
                                                                                                                            cu14.setOrderQuantity(cu14.getOrderQuantity() + orderQuantity);
                                                                                                                            customerDao.update(cu14);
                                                                                                                            handleAccount2(cu14.getId(), totalAmount, cu14.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                                            if (cu14.getParentId() != null && cu14.getParentId() != 1198 && cu14.getLevelCopyId() != 11) {

                                                                                                                                Customer cu15 = customerDao.queryById(cu14.getParentId()); // 十五层
                                                                                                                                if (cu15 != null) {
                                                                                                                                    cu15.setAchievement3(cu15.getAchievement3().add(totalAmount));
                                                                                                                                    cu15.setOrderQuantity(cu15.getOrderQuantity() + orderQuantity);
                                                                                                                                    customerDao.update(cu15);
                                                                                                                                    handleAccount2(cu15.getId(), totalAmount, cu15.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                                                    if (cu15.getParentId() != null && cu15.getParentId() != 1198 && cu15.getLevelCopyId() != 11) {

                                                                                                                                        Customer cu16 = customerDao.queryById(cu15.getParentId()); // 十六层
                                                                                                                                        if (cu16 != null) {
                                                                                                                                            cu16.setAchievement3(cu16.getAchievement3().add(totalAmount));
                                                                                                                                            cu16.setOrderQuantity(cu16.getOrderQuantity() + orderQuantity);
                                                                                                                                            customerDao.update(cu16);
                                                                                                                                            handleAccount2(cu16.getId(), totalAmount, cu16.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                                                            if (cu16.getParentId() != null && cu16.getParentId() != 1198 && cu16.getLevelCopyId() != 11) {

                                                                                                                                                Customer cu17 = customerDao.queryById(cu16.getParentId()); // 十七层
                                                                                                                                                if (cu17 != null) {
                                                                                                                                                    cu17.setAchievement3(cu17.getAchievement3().add(totalAmount));
                                                                                                                                                    cu17.setOrderQuantity(cu17.getOrderQuantity() + orderQuantity);
                                                                                                                                                    customerDao.update(cu17);
                                                                                                                                                    handleAccount2(cu17.getId(), totalAmount, cu17.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                                                                    if (cu17.getParentId() != null && cu17.getParentId() != 1198 && cu17.getLevelCopyId() != 11) {

                                                                                                                                                        Customer cu18 = customerDao.queryById(cu17.getParentId()); // 十八层
                                                                                                                                                        if(cu18 != null){
                                                                                                                                                            cu18.setAchievement3(cu18.getAchievement3().add(totalAmount));
                                                                                                                                                            cu18.setOrderQuantity(cu18.getOrderQuantity() + orderQuantity);
                                                                                                                                                            customerDao.update(cu18);
                                                                                                                                                            handleAccount2(cu18.getId(), totalAmount, cu18.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                                                                            if (cu18.getParentId() != null && cu18.getParentId() != 1198 && cu18.getLevelCopyId() != 11) {

                                                                                                                                                                Customer cu19 = customerDao.queryById(cu18.getParentId()); // 十九层
                                                                                                                                                                if(cu19 != null){
                                                                                                                                                                    cu19.setAchievement3(cu19.getAchievement3().add(totalAmount));
                                                                                                                                                                    cu19.setOrderQuantity(cu19.getOrderQuantity() + orderQuantity);
                                                                                                                                                                    customerDao.update(cu19);
                                                                                                                                                                    handleAccount2(cu19.getId(), totalAmount, cu19.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                                                                                    if (cu19.getParentId() != null && cu19.getParentId() != 1198 && cu19.getLevelCopyId() != 11) {

                                                                                                                                                                        Customer cu20 = customerDao.queryById(cu19.getParentId()); // 二十层
                                                                                                                                                                        if(cu20 != null){
                                                                                                                                                                            cu20.setAchievement3(cu20.getAchievement3().add(totalAmount));
                                                                                                                                                                            cu20.setOrderQuantity(cu20.getOrderQuantity() + orderQuantity);
                                                                                                                                                                            customerDao.update(cu20);
                                                                                                                                                                            handleAccount2(cu20.getId(), totalAmount, cu20.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                                                                                            if (cu20.getParentId() != null && cu20.getParentId() != 1198 && cu20.getLevelCopyId() != 11) {

                                                                                                                                                                                Customer cu21 = customerDao.queryById(cu20.getParentId()); // 二十一层
                                                                                                                                                                                if(cu21 != null){
                                                                                                                                                                                    cu21.setAchievement3(cu21.getAchievement3().add(totalAmount));
                                                                                                                                                                                    cu21.setOrderQuantity(cu21.getOrderQuantity() + orderQuantity);
                                                                                                                                                                                    customerDao.update(cu21);
                                                                                                                                                                                    handleAccount2(cu21.getId(), totalAmount, cu21.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                                                                                                    if (cu21.getParentId() != null && cu21.getParentId() != 1198 && cu21.getLevelCopyId() != 11) {

                                                                                                                                                                                        Customer cu22 = customerDao.queryById(cu21.getParentId()); // 二十二层
                                                                                                                                                                                        if(cu22 != null){
                                                                                                                                                                                            cu22.setAchievement3(cu22.getAchievement3().add(totalAmount));
                                                                                                                                                                                            cu22.setOrderQuantity(cu22.getOrderQuantity() + orderQuantity);
                                                                                                                                                                                            customerDao.update(cu22);
                                                                                                                                                                                            handleAccount2(cu22.getId(), totalAmount, cu22.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                                                                                                            if (cu22.getParentId() != null && cu22.getParentId() != 1198 && cu22.getLevelCopyId() != 11) {

                                                                                                                                                                                                Customer cu23 = customerDao.queryById(cu22.getParentId()); // 二十三层
                                                                                                                                                                                                if(cu23 != null){
                                                                                                                                                                                                    cu23.setAchievement3(cu23.getAchievement3().add(totalAmount));
                                                                                                                                                                                                    cu23.setOrderQuantity(cu23.getOrderQuantity() + orderQuantity);
                                                                                                                                                                                                    customerDao.update(cu23);
                                                                                                                                                                                                    handleAccount2(cu23.getId(), totalAmount, cu23.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                                                                                                                    if (cu23.getParentId() != null && cu23.getParentId() != 1198 && cu23.getLevelCopyId() != 11) {

                                                                                                                                                                                                        Customer cu24 = customerDao.queryById(cu23.getParentId()); // 二十四层
                                                                                                                                                                                                        if(cu24 != null){
                                                                                                                                                                                                            cu24.setAchievement3(cu24.getAchievement3().add(totalAmount));
                                                                                                                                                                                                            cu24.setOrderQuantity(cu24.getOrderQuantity() + orderQuantity);
                                                                                                                                                                                                            customerDao.update(cu24);
                                                                                                                                                                                                            handleAccount2(cu24.getId(), totalAmount, cu24.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                                                                                                                            if (cu24.getParentId() != null && cu24.getParentId() != 1198 && cu24.getLevelCopyId() != 11) {

                                                                                                                                                                                                                Customer cu25 = customerDao.queryById(cu24.getParentId()); // 二十五层
                                                                                                                                                                                                                if(cu25 != null){
                                                                                                                                                                                                                    cu25.setAchievement3(cu25.getAchievement3().add(totalAmount));
                                                                                                                                                                                                                    cu25.setOrderQuantity(cu25.getOrderQuantity() + orderQuantity);
                                                                                                                                                                                                                    customerDao.update(cu25);
                                                                                                                                                                                                                    handleAccount2(cu25.getId(), totalAmount, cu25.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());

                                                                                                                                                                                                                    if (cu25.getParentId() != null && cu25.getParentId() != 1198 && cu25.getLevelCopyId() != 11) {

                                                                                                                                                                                                                        Customer cu26 = customerDao.queryById(cu25.getParentId()); // 二十六层
                                                                                                                                                                                                                        if(cu26 != null){
                                                                                                                                                                                                                            cu26.setAchievement3(cu26.getAchievement3().add(totalAmount));
                                                                                                                                                                                                                            cu26.setOrderQuantity(cu26.getOrderQuantity() + orderQuantity);
                                                                                                                                                                                                                            customerDao.update(cu26);
                                                                                                                                                                                                                            handleAccount2(cu26.getId(), totalAmount, cu26.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());

                                                                                                                                                                                                                            if (cu26.getParentId() != null && cu26.getParentId() != 1198 && cu26.getLevelCopyId() != 11) {

                                                                                                                                                                                                                                Customer cu27 = customerDao.queryById(cu26.getParentId()); // 二十七层
                                                                                                                                                                                                                                if(cu27 != null){
                                                                                                                                                                                                                                    cu27.setAchievement3(cu27.getAchievement3().add(totalAmount));
                                                                                                                                                                                                                                    cu27.setOrderQuantity(cu27.getOrderQuantity() + orderQuantity);
                                                                                                                                                                                                                                    customerDao.update(cu27);
                                                                                                                                                                                                                                    handleAccount2(cu27.getId(), totalAmount, cu27.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());

                                                                                                                                                                                                                                    if (cu27.getParentId() != null && cu27.getParentId() != 1198 && cu27.getLevelCopyId() != 11) {

                                                                                                                                                                                                                                        Customer cu28 = customerDao.queryById(cu27.getParentId()); // 二十八层
                                                                                                                                                                                                                                        if(cu28 != null){
                                                                                                                                                                                                                                            cu28.setAchievement3(cu28.getAchievement3().add(totalAmount));
                                                                                                                                                                                                                                            cu28.setOrderQuantity(cu28.getOrderQuantity() + orderQuantity);
                                                                                                                                                                                                                                            customerDao.update(cu28);
                                                                                                                                                                                                                                            handleAccount2(cu28.getId(), totalAmount, cu28.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());

                                                                                                                                                                                                                                            if (cu28.getParentId() != null && cu28.getParentId() != 1198 && cu28.getLevelCopyId() != 11) {

                                                                                                                                                                                                                                                Customer cu29 = customerDao.queryById(cu28.getParentId()); // 二十九层
                                                                                                                                                                                                                                                if(cu29 != null){
                                                                                                                                                                                                                                                    cu29.setAchievement3(cu29.getAchievement3().add(totalAmount));
                                                                                                                                                                                                                                                    cu29.setOrderQuantity(cu29.getOrderQuantity() + orderQuantity);
                                                                                                                                                                                                                                                    customerDao.update(cu29);
                                                                                                                                                                                                                                                    handleAccount2(cu29.getId(), totalAmount, cu29.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());

                                                                                                                                                                                                                                                    if (cu29.getParentId() != null && cu29.getParentId() != 1198 && cu29.getLevelCopyId() != 11) {

                                                                                                                                                                                                                                                        Customer cu30 = customerDao.queryById(cu29.getParentId()); // 三十层
                                                                                                                                                                                                                                                        if(cu30 != null){
                                                                                                                                                                                                                                                            cu30.setAchievement3(cu30.getAchievement3().add(totalAmount));
                                                                                                                                                                                                                                                            cu30.setOrderQuantity(cu30.getOrderQuantity() + orderQuantity);
                                                                                                                                                                                                                                                            customerDao.update(cu30);
                                                                                                                                                                                                                                                            handleAccount2(cu30.getId(), totalAmount, cu30.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());

                                                                                                                                                                                                                                                            if (cu30.getParentId() != null && cu30.getParentId() != 1198 && cu30.getLevelCopyId() != 11) {

                                                                                                                                                                                                                                                                Customer cu31 = customerDao.queryById(cu30.getParentId()); // 三十一层
                                                                                                                                                                                                                                                                if(cu31 != null){
                                                                                                                                                                                                                                                                    cu31.setAchievement3(cu31.getAchievement3().add(totalAmount));
                                                                                                                                                                                                                                                                    cu31.setOrderQuantity(cu31.getOrderQuantity() + orderQuantity);
                                                                                                                                                                                                                                                                    customerDao.update(cu31);
                                                                                                                                                                                                                                                                    handleAccount2(cu31.getId(), totalAmount, cu31.getAchievement3(), orders.getOrderNo(), customer.getAgentCode());
                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                }
                                                                                                                                                                                                                            }
                                                                                                                                                                                                                        }
                                                                                                                                                                                                                    }
                                                                                                                                                                                                                }
                                                                                                                                                                                                            }
                                                                                                                                                                                                        }
                                                                                                                                                                                                    }
                                                                                                                                                                                                }
                                                                                                                                                                                            }
                                                                                                                                                                                        }
                                                                                                                                                                                    }
                                                                                                                                                                                }
                                                                                                                                                                            }
                                                                                                                                                                        }
                                                                                                                                                                    }
                                                                                                                                                                }
                                                                                                                                                            }
                                                                                                                                                        }
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }


    /**
     * 共享模式 分润
     * @param customer
     * @param orders
     */
    public void shareSplitProfit(Customer customer, Orders orders){
        // 分润
        Integer quantity = 0;
        if(orders.getTotalAmount().doubleValue() >= 369 && orders.getTotalAmount().doubleValue() < 3690){
            quantity = 1;
        }else if(orders.getTotalAmount().doubleValue() >= 3690 && orders.getTotalAmount().doubleValue() < 36900){
            quantity = 10;
        }else if(orders.getTotalAmount().doubleValue() >= 36900){
            quantity = 100;
        }
        // V等级分润
        if(customer.getParentId() != null && customer.getParentId() != 1198){
            Customer cu1 = customerDao.queryById(customer.getParentId());
            if(cu1 != null){
                Integer orderLevel = customer.getLevelCopy4Id();
                // 下单人等级 小于 上级
                if(orderLevel < cu1.getLevelCopy4Id()){
                    // 直接分钱
                    if(cu1.getLevelCopy4Id() == 2){
                        BigDecimal bd = new BigDecimal(15);
                        handleAccount1(cu1.getId(), BigDecimal.valueOf(15), orders.getOrderNo(), customer.getAgentCode(), quantity);
                        // 找一个就近为2的
                        Customer nearby2 = findCustomer_nearby(cu1, 2);
                        if(nearby2 != null){
                            handleAccount1(nearby2.getId(), bd.multiply(BigDecimal.valueOf(0.2)), orders.getOrderNo(), customer.getAgentCode(), quantity);
                        }
                        // 继续找大于2的
                        Customer cu2 = findParentUser_nearby(cu1, 2);
                        if(cu2 != null){
                            if(cu2.getLevelCopy4Id() == 3){
                                handleAccount1(cu2.getId(), BigDecimal.valueOf(15), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为3的
                                Customer nearby3 = findCustomer_nearby(cu2, 3);
                                if(nearby3 != null){
                                    handleAccount1(nearby3.getId(), BigDecimal.valueOf(15 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于3的
                                Customer cu3 = findParentUser_nearby(cu2, 3);
                                if(cu3 != null){
                                    if(cu3.getLevelCopy4Id() == 4){
                                        handleAccount1(cu3.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为4的
                                        Customer nearby4 = findCustomer_nearby(cu3, 4);
                                        if(nearby4 != null){
                                            handleAccount1(nearby4.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于4的
                                        Customer cu4 = findParentUser_nearby(cu3, 4);
                                        if(cu4 != null){
                                            if(cu4.getLevelCopy4Id() == 5){
                                                handleAccount1(cu4.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为5的
                                                Customer nearby5 = findCustomer_nearby(cu4, 5);
                                                if(nearby5 != null){
                                                    handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于5的
                                                Customer cu5 = findParentUser_nearby(cu4, 5);
                                                if(cu5 != null){
                                                    if(cu5.getLevelCopy4Id() == 6){
                                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        // 找一个就近为6的
                                                        Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                        if(nearby6 != null){
                                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        }
                                                        // 继续找大于6的
                                                        Customer cu6 = findParentUser_nearby(cu5, 6);
                                                        if(cu6 != null){
                                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                            // 找一个就近为7的
                                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                            if(nearby7 != null){
                                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                            }
                                                        }
                                                    }else if(cu5.getLevelCopy4Id() == 7){
                                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        // 找一个就近为7的
                                                        Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                        if(nearby7 != null){
                                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        }
                                                    }
                                                }
                                            }else if(cu4.getLevelCopy4Id() == 6){
                                                handleAccount1(cu4.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu4, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu4, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu4.getLevelCopy4Id() == 7){
                                                handleAccount1(cu4.getId(), BigDecimal.valueOf(33), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu4, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(33 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu3.getLevelCopy4Id() == 5){
                                        handleAccount1(cu3.getId(), BigDecimal.valueOf(26), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为5的
                                        Customer nearby5 = findCustomer_nearby(cu3, 5);
                                        if(nearby5 != null){
                                            handleAccount1(nearby5.getId(), BigDecimal.valueOf(26 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于5的
                                        Customer cu5 = findParentUser_nearby(cu3, 5);
                                        if(cu5 != null){
                                            if(cu5.getLevelCopy4Id() == 6){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu5, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu5.getLevelCopy4Id() == 7){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu3.getLevelCopy4Id() == 6){
                                        handleAccount1(cu3.getId(), BigDecimal.valueOf(37), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu3, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(37 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu3, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu3.getLevelCopy4Id() == 7){
                                        handleAccount1(cu3.getId(), BigDecimal.valueOf(48), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu3, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(48 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu2.getLevelCopy4Id() == 4){
                                handleAccount1(cu2.getId(), BigDecimal.valueOf(30), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为4的
                                Customer nearby4 = findCustomer_nearby(cu2, 4);
                                if(nearby4 != null){
                                    handleAccount1(nearby4.getId(), BigDecimal.valueOf(30 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于4的
                                Customer cu4 = findParentUser_nearby(cu2, 4);
                                if(cu4 != null){
                                    if(cu4.getLevelCopy4Id() == 5){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为5的
                                        Customer nearby5 = findCustomer_nearby(cu4, 5);
                                        if(nearby5 != null){
                                            handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于5的
                                        Customer cu5 = findParentUser_nearby(cu4, 5);
                                        if(cu5 != null){
                                            if(cu5.getLevelCopy4Id() == 6){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu5, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu5.getLevelCopy4Id() == 7){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu4.getLevelCopy4Id() == 6){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu4, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu4, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu4.getLevelCopy4Id() == 7){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(33), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu4, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(33 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu2.getLevelCopy4Id() == 5){
                                handleAccount1(cu2.getId(), BigDecimal.valueOf(41), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为5的
                                Customer nearby5 = findCustomer_nearby(cu2, 5);
                                if(nearby5 != null){
                                    handleAccount1(nearby5.getId(), BigDecimal.valueOf(41 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于5的
                                Customer cu5 = findParentUser_nearby(cu2, 5);
                                if(cu5 != null){
                                    if(cu5.getLevelCopy4Id() == 6){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu5, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu5, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu5.getLevelCopy4Id() == 7){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu5, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu2.getLevelCopy4Id() == 6){
                                handleAccount1(cu2.getId(), BigDecimal.valueOf(52), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为6的
                                Customer nearby6 = findCustomer_nearby(cu2, 6);
                                if(nearby6 != null){
                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(52 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于6的
                                Customer cu6 = findParentUser_nearby(cu2, 6);
                                if(cu6 != null){
                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    // 找一个就近为7的
                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                    if(nearby7 != null){
                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    }
                                }
                            }else if(cu2.getLevelCopy4Id() == 7){
                                handleAccount1(cu2.getId(), BigDecimal.valueOf(63), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为7的
                                Customer nearby7 = findCustomer_nearby(cu2, 7);
                                if(nearby7 != null){
                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(63 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                            }
                        }
                    }else if(cu1.getLevelCopy4Id() == 3){
                        BigDecimal bd = new BigDecimal(30);
                        //if(orderLevel == 2){
                            //bd = new BigDecimal(15);
                        //}
                        handleAccount1(cu1.getId(), bd, orders.getOrderNo(), customer.getAgentCode(), quantity);
                        // 找一个就近为3的
                        Customer nearby3 = findCustomer_nearby(cu1, 3);
                        if(nearby3 != null){
                            handleAccount1(nearby3.getId(), bd.multiply(BigDecimal.valueOf(0.2)), orders.getOrderNo(), customer.getAgentCode(), quantity);
                        }
                        // 继续找大于3的
                        Customer cu3 = findParentUser_nearby(cu1, 3);
                        if(cu3 != null){
                            if(cu3.getLevelCopy4Id() == 4){
                                handleAccount1(cu3.getId(), BigDecimal.valueOf(15), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为4的
                                Customer nearby4 = findCustomer_nearby(cu3, 4);
                                if(nearby4 != null){
                                    handleAccount1(nearby4.getId(), BigDecimal.valueOf(15 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于4的
                                Customer cu4 = findParentUser_nearby(cu3, 4);
                                if(cu4 != null){
                                    if(cu4.getLevelCopy4Id() == 5){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为5的
                                        Customer nearby5 = findCustomer_nearby(cu4, 5);
                                        if(nearby5 != null){
                                            handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于5的
                                        Customer cu5 = findParentUser_nearby(cu4, 5);
                                        if(cu5 != null){
                                            if(cu5.getLevelCopy4Id() == 6){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu5, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu5.getLevelCopy4Id() == 7){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu4.getLevelCopy4Id() == 6){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu4, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu4, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu4.getLevelCopy4Id() == 7){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(33), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu4, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(33 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu3.getLevelCopy4Id() == 5){
                                handleAccount1(cu3.getId(), BigDecimal.valueOf(26), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为5的
                                Customer nearby5 = findCustomer_nearby(cu3, 5);
                                if(nearby5 != null){
                                    handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于5的
                                Customer cu5 = findParentUser_nearby(cu3, 5);
                                if(cu5 != null){
                                    if(cu5.getLevelCopy4Id() == 6){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu5, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu5, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu5.getLevelCopy4Id() == 7){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu5, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu3.getLevelCopy4Id() == 6){
                                handleAccount1(cu3.getId(), BigDecimal.valueOf(37), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为6的
                                Customer nearby6 = findCustomer_nearby(cu3, 6);
                                if(nearby6 != null){
                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(37 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于6的
                                Customer cu6 = findParentUser_nearby(cu3, 6);
                                if(cu6 != null){
                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    // 找一个就近为7的
                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                    if(nearby7 != null){
                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    }
                                }
                            }else if(cu3.getLevelCopy4Id() == 7){
                                handleAccount1(cu3.getId(), BigDecimal.valueOf(48), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为7的
                                Customer nearby7 = findCustomer_nearby(cu3, 7);
                                if(nearby7 != null){
                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(48 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                            }
                        }
                    }else if(cu1.getLevelCopy4Id() == 4){
                        BigDecimal bd = new BigDecimal(45);
                        //if(orderLevel == 2){
                            //bd = new BigDecimal(30);
                        //}else if(orderLevel == 3){
                            //bd = new BigDecimal(15);
                        //}
                        handleAccount1(cu1.getId(), bd, orders.getOrderNo(), customer.getAgentCode(), quantity);
                        // 找一个就近为4的
                        Customer nearby4 = findCustomer_nearby(cu1, 4);
                        if(nearby4 != null){
                            handleAccount1(nearby4.getId(), bd.multiply(BigDecimal.valueOf(0.2)), orders.getOrderNo(), customer.getAgentCode(), quantity);
                        }
                        // 继续找大于4的
                        Customer cu4 = findParentUser_nearby(cu1, 4);
                        if(cu4 != null){
                            if(cu4.getLevelCopy4Id() == 5){
                                handleAccount1(cu4.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为5的
                                Customer nearby5 = findCustomer_nearby(cu4, 5);
                                if(nearby5 != null){
                                    handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于5的
                                Customer cu5 = findParentUser_nearby(cu4, 5);
                                if(cu5 != null){
                                    if(cu5.getLevelCopy4Id() == 6){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu5, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu5, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu5.getLevelCopy4Id() == 7){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu5, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu4.getLevelCopy4Id() == 6){
                                handleAccount1(cu4.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为6的
                                Customer nearby6 = findCustomer_nearby(cu4, 6);
                                if(nearby6 != null){
                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于6的
                                Customer cu6 = findParentUser_nearby(cu4, 6);
                                if(cu6 != null){
                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    // 找一个就近为7的
                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                    if(nearby7 != null){
                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    }
                                }
                            }else if(cu4.getLevelCopy4Id() == 7){
                                handleAccount1(cu4.getId(), BigDecimal.valueOf(33), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为7的
                                Customer nearby7 = findCustomer_nearby(cu4, 7);
                                if(nearby7 != null){
                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(33 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                            }
                        }
                    }else if(cu1.getLevelCopy4Id() == 5){
                        BigDecimal bd = new BigDecimal(56);
                        //if(orderLevel == 2){
                            //bd = new BigDecimal(41);
                        //}else if(orderLevel == 3){
                            //bd = new BigDecimal(26);
                        //}else if(orderLevel == 4){
                            //bd = new BigDecimal(11);
                        //}
                        handleAccount1(cu1.getId(), bd, orders.getOrderNo(), customer.getAgentCode(), quantity);
                        // 找一个就近为5的
                        Customer nearby5 = findCustomer_nearby(cu1, 5);
                        if(nearby5 != null){
                            handleAccount1(nearby5.getId(), bd.multiply(BigDecimal.valueOf(0.2)), orders.getOrderNo(), customer.getAgentCode(), quantity);
                        }
                        // 继续找大于5的
                        Customer cu5 = findParentUser_nearby(cu1, 5);
                        if(cu5 != null){
                            if(cu5.getLevelCopy4Id() == 6){
                                handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为6的
                                Customer nearby6 = findCustomer_nearby(cu5, 6);
                                if(nearby6 != null){
                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于6的
                                Customer cu6 = findParentUser_nearby(cu5, 6);
                                if(cu6 != null){
                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    // 找一个就近为7的
                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                    if(nearby7 != null){
                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    }
                                }
                            }else if(cu5.getLevelCopy4Id() == 7){
                                handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为7的
                                Customer nearby7 = findCustomer_nearby(cu5, 7);
                                if(nearby7 != null){
                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                            }
                        }
                    }else if(cu1.getLevelCopy4Id() == 6){
                        BigDecimal bd = new BigDecimal(67);
                        //if(orderLevel == 2){
                            //bd = new BigDecimal(52);
                        //}else if(orderLevel == 3){
                            //bd = new BigDecimal(37);
                        //}else if(orderLevel == 4){
                            //bd = new BigDecimal(22);
                        //}else if(orderLevel == 5){
                            //bd = new BigDecimal(11);
                        //}
                        handleAccount1(cu1.getId(), bd, orders.getOrderNo(), customer.getAgentCode(), quantity);
                        // 找一个就近为6的
                        Customer nearby6 = findCustomer_nearby(cu1, 6);
                        if(nearby6 != null){
                            handleAccount1(nearby6.getId(), bd.multiply(BigDecimal.valueOf(0.2)), orders.getOrderNo(), customer.getAgentCode(), quantity);
                        }
                        // 继续找大于6的
                        Customer cu6 = findParentUser_nearby(cu1, 6);
                        if(cu6 != null){
                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                            // 找一个就近为7的
                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                            if(nearby7 != null){
                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                            }
                        }
                    }else if(cu1.getLevelCopy4Id() == 7){
                        BigDecimal bd = new BigDecimal(78);
                        //if(orderLevel == 2){
                            //bd = new BigDecimal(63);
                        //}else if(orderLevel == 3){
                            //bd = new BigDecimal(48);
                        //}else if(orderLevel == 4){
                            //bd = new BigDecimal(33);
                        //}else if(orderLevel == 5){
                            //bd = new BigDecimal(22);
                        //}else if(orderLevel == 6){
                            //bd = new BigDecimal(11);
                        //}
                        handleAccount1(cu1.getId(), bd, orders.getOrderNo(), customer.getAgentCode(), quantity);
                        // 找一个就近为7的
                        Customer nearby7 = findCustomer_nearby(cu1, 7);
                        if(nearby7 != null){
                            handleAccount1(nearby7.getId(), bd.multiply(BigDecimal.valueOf(0.2)), orders.getOrderNo(), customer.getAgentCode(), quantity);
                        }
                    }
                }
                // 下单人等级 大于等于 上级
                else if(orderLevel >= cu1.getLevelCopy4Id()){
                    if(orderLevel == 1){
                        // 继续找大于1的
                        Customer cu1_1 = findParentUser_nearby(customer, 1);
                        if(cu1_1 != null){
                            if(cu1_1.getLevelCopy4Id() == 2){
                                BigDecimal bd = new BigDecimal(15);
                                handleAccount1(cu1_1.getId(), BigDecimal.valueOf(15), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为2的
                                Customer nearby2 = findCustomer_nearby(cu1_1, 2);
                                if(nearby2 != null){
                                    handleAccount1(nearby2.getId(), bd.multiply(BigDecimal.valueOf(0.2)), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于2的
                                Customer cu2 = findParentUser_nearby(cu1_1, 2);
                                if(cu2 != null){
                                    if(cu2.getLevelCopy4Id() == 3){
                                        handleAccount1(cu2.getId(), BigDecimal.valueOf(15), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为3的
                                        Customer nearby3 = findCustomer_nearby(cu2, 3);
                                        if(nearby3 != null){
                                            handleAccount1(nearby3.getId(), BigDecimal.valueOf(15 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于3的
                                        Customer cu3 = findParentUser_nearby(cu2, 3);
                                        if(cu3 != null){
                                            if(cu3.getLevelCopy4Id() == 4){
                                                handleAccount1(cu3.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为4的
                                                Customer nearby4 = findCustomer_nearby(cu3, 4);
                                                if(nearby4 != null){
                                                    handleAccount1(nearby4.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于4的
                                                Customer cu4 = findParentUser_nearby(cu3, 4);
                                                if(cu4 != null){
                                                    if(cu4.getLevelCopy4Id() == 5){
                                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        // 找一个就近为5的
                                                        Customer nearby5 = findCustomer_nearby(cu4, 5);
                                                        if(nearby5 != null){
                                                            handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        }
                                                        // 继续找大于5的
                                                        Customer cu5 = findParentUser_nearby(cu4, 5);
                                                        if(cu5 != null){
                                                            if(cu5.getLevelCopy4Id() == 6){
                                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                                // 找一个就近为6的
                                                                Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                                if(nearby6 != null){
                                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                                }
                                                                // 继续找大于6的
                                                                Customer cu6 = findParentUser_nearby(cu5, 6);
                                                                if(cu6 != null){
                                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                                    // 找一个就近为7的
                                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                                    if(nearby7 != null){
                                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                                    }
                                                                }
                                                            }else if(cu5.getLevelCopy4Id() == 7){
                                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                                // 找一个就近为7的
                                                                Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                                if(nearby7 != null){
                                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                                }
                                                            }
                                                        }
                                                    }else if(cu4.getLevelCopy4Id() == 6){
                                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        // 找一个就近为6的
                                                        Customer nearby6 = findCustomer_nearby(cu4, 6);
                                                        if(nearby6 != null){
                                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        }
                                                        // 继续找大于6的
                                                        Customer cu6 = findParentUser_nearby(cu4, 6);
                                                        if(cu6 != null){
                                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                            // 找一个就近为7的
                                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                            if(nearby7 != null){
                                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                            }
                                                        }
                                                    }else if(cu4.getLevelCopy4Id() == 7){
                                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(33), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        // 找一个就近为7的
                                                        Customer nearby7 = findCustomer_nearby(cu4, 7);
                                                        if(nearby7 != null){
                                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(33 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        }
                                                    }
                                                }
                                            }else if(cu3.getLevelCopy4Id() == 5){
                                                handleAccount1(cu3.getId(), BigDecimal.valueOf(26), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为5的
                                                Customer nearby5 = findCustomer_nearby(cu3, 5);
                                                if(nearby5 != null){
                                                    handleAccount1(nearby5.getId(), BigDecimal.valueOf(26 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于5的
                                                Customer cu5 = findParentUser_nearby(cu3, 5);
                                                if(cu5 != null){
                                                    if(cu5.getLevelCopy4Id() == 6){
                                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        // 找一个就近为6的
                                                        Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                        if(nearby6 != null){
                                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        }
                                                        // 继续找大于6的
                                                        Customer cu6 = findParentUser_nearby(cu5, 6);
                                                        if(cu6 != null){
                                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                            // 找一个就近为7的
                                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                            if(nearby7 != null){
                                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                            }
                                                        }
                                                    }else if(cu5.getLevelCopy4Id() == 7){
                                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        // 找一个就近为7的
                                                        Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                        if(nearby7 != null){
                                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        }
                                                    }
                                                }
                                            }else if(cu3.getLevelCopy4Id() == 6){
                                                handleAccount1(cu3.getId(), BigDecimal.valueOf(37), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu3, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(37 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu3, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu3.getLevelCopy4Id() == 7){
                                                handleAccount1(cu3.getId(), BigDecimal.valueOf(48), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu3, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(48 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu2.getLevelCopy4Id() == 4){
                                        handleAccount1(cu2.getId(), BigDecimal.valueOf(30), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为4的
                                        Customer nearby4 = findCustomer_nearby(cu2, 4);
                                        if(nearby4 != null){
                                            handleAccount1(nearby4.getId(), BigDecimal.valueOf(30 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于4的
                                        Customer cu4 = findParentUser_nearby(cu2, 4);
                                        if(cu4 != null){
                                            if(cu4.getLevelCopy4Id() == 5){
                                                handleAccount1(cu4.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为5的
                                                Customer nearby5 = findCustomer_nearby(cu4, 5);
                                                if(nearby5 != null){
                                                    handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于5的
                                                Customer cu5 = findParentUser_nearby(cu4, 5);
                                                if(cu5 != null){
                                                    if(cu5.getLevelCopy4Id() == 6){
                                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        // 找一个就近为6的
                                                        Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                        if(nearby6 != null){
                                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        }
                                                        // 继续找大于6的
                                                        Customer cu6 = findParentUser_nearby(cu5, 6);
                                                        if(cu6 != null){
                                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                            // 找一个就近为7的
                                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                            if(nearby7 != null){
                                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                            }
                                                        }
                                                    }else if(cu5.getLevelCopy4Id() == 7){
                                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        // 找一个就近为7的
                                                        Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                        if(nearby7 != null){
                                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        }
                                                    }
                                                }
                                            }else if(cu4.getLevelCopy4Id() == 6){
                                                handleAccount1(cu4.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu4, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu4, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu4.getLevelCopy4Id() == 7){
                                                handleAccount1(cu4.getId(), BigDecimal.valueOf(33), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu4, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(33 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu2.getLevelCopy4Id() == 5){
                                        handleAccount1(cu2.getId(), BigDecimal.valueOf(41), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为5的
                                        Customer nearby5 = findCustomer_nearby(cu2, 5);
                                        if(nearby5 != null){
                                            handleAccount1(nearby5.getId(), BigDecimal.valueOf(41 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于5的
                                        Customer cu5 = findParentUser_nearby(cu2, 5);
                                        if(cu5 != null){
                                            if(cu5.getLevelCopy4Id() == 6){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu5, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu5.getLevelCopy4Id() == 7){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu2.getLevelCopy4Id() == 6){
                                        handleAccount1(cu2.getId(), BigDecimal.valueOf(52), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu2, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(52 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu2, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu2.getLevelCopy4Id() == 7){
                                        handleAccount1(cu2.getId(), BigDecimal.valueOf(63), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu2, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(63 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu1_1.getLevelCopy4Id() == 3){
                                BigDecimal bd = new BigDecimal(30);
                                //if(orderLevel == 2){
                                    //bd = new BigDecimal(15);
                                //}
                                handleAccount1(cu1_1.getId(), bd, orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为3的
                                Customer nearby3 = findCustomer_nearby(cu1_1, 3);
                                if(nearby3 != null){
                                    handleAccount1(nearby3.getId(), bd.multiply(BigDecimal.valueOf(0.2)), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于3的
                                Customer cu3 = findParentUser_nearby(cu1_1, 3);
                                if(cu3 != null){
                                    if(cu3.getLevelCopy4Id() == 4){
                                        handleAccount1(cu3.getId(), BigDecimal.valueOf(15), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为4的
                                        Customer nearby4 = findCustomer_nearby(cu3, 4);
                                        if(nearby4 != null){
                                            handleAccount1(nearby4.getId(), BigDecimal.valueOf(15 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于4的
                                        Customer cu4 = findParentUser_nearby(cu3, 4);
                                        if(cu4 != null){
                                            if(cu4.getLevelCopy4Id() == 5){
                                                handleAccount1(cu4.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为5的
                                                Customer nearby5 = findCustomer_nearby(cu4, 5);
                                                if(nearby5 != null){
                                                    handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于5的
                                                Customer cu5 = findParentUser_nearby(cu4, 5);
                                                if(cu5 != null){
                                                    if(cu5.getLevelCopy4Id() == 6){
                                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        // 找一个就近为6的
                                                        Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                        if(nearby6 != null){
                                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        }
                                                        // 继续找大于6的
                                                        Customer cu6 = findParentUser_nearby(cu5, 6);
                                                        if(cu6 != null){
                                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                            // 找一个就近为7的
                                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                            if(nearby7 != null){
                                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                            }
                                                        }
                                                    }else if(cu5.getLevelCopy4Id() == 7){
                                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        // 找一个就近为7的
                                                        Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                        if(nearby7 != null){
                                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        }
                                                    }
                                                }
                                            }else if(cu4.getLevelCopy4Id() == 6){
                                                handleAccount1(cu4.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu4, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu4, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu4.getLevelCopy4Id() == 7){
                                                handleAccount1(cu4.getId(), BigDecimal.valueOf(33), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu4, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(33 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu3.getLevelCopy4Id() == 5){
                                        handleAccount1(cu3.getId(), BigDecimal.valueOf(26), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为5的
                                        Customer nearby5 = findCustomer_nearby(cu3, 5);
                                        if(nearby5 != null){
                                            handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于5的
                                        Customer cu5 = findParentUser_nearby(cu3, 5);
                                        if(cu5 != null){
                                            if(cu5.getLevelCopy4Id() == 6){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu5, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu5.getLevelCopy4Id() == 7){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu3.getLevelCopy4Id() == 6){
                                        handleAccount1(cu3.getId(), BigDecimal.valueOf(37), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu3, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(37 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu3, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu3.getLevelCopy4Id() == 7){
                                        handleAccount1(cu3.getId(), BigDecimal.valueOf(48), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu3, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(48 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu1_1.getLevelCopy4Id() == 4){
                                BigDecimal bd = new BigDecimal(45);
                                //if(orderLevel == 2){
                                    //bd = new BigDecimal(30);
                                //}else if(orderLevel == 3){
                                    //bd = new BigDecimal(15);
                                //}
                                handleAccount1(cu1_1.getId(), bd, orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为4的
                                Customer nearby4 = findCustomer_nearby(cu1_1, 4);
                                if(nearby4 != null){
                                    handleAccount1(nearby4.getId(), bd.multiply(BigDecimal.valueOf(0.2)), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于4的
                                Customer cu4 = findParentUser_nearby(cu1_1, 4);
                                if(cu4 != null){
                                    if(cu4.getLevelCopy4Id() == 5){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为5的
                                        Customer nearby5 = findCustomer_nearby(cu4, 5);
                                        if(nearby5 != null){
                                            handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于5的
                                        Customer cu5 = findParentUser_nearby(cu4, 5);
                                        if(cu5 != null){
                                            if(cu5.getLevelCopy4Id() == 6){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu5, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu5.getLevelCopy4Id() == 7){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu4.getLevelCopy4Id() == 6){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu4, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu4, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu4.getLevelCopy4Id() == 7){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(33), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu4, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(33 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu1_1.getLevelCopy4Id() == 5){
                                BigDecimal bd = new BigDecimal(56);
                                //if(orderLevel == 2){
                                    //bd = new BigDecimal(41);
                                //}else if(orderLevel == 3){
                                    //bd = new BigDecimal(26);
                                //}else if(orderLevel == 4){
                                    //bd = new BigDecimal(11);
                                //}
                                handleAccount1(cu1_1.getId(), bd, orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为5的
                                Customer nearby5 = findCustomer_nearby(cu1_1, 5);
                                if(nearby5 != null){
                                    handleAccount1(nearby5.getId(), bd.multiply(BigDecimal.valueOf(0.2)), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于5的
                                Customer cu5 = findParentUser_nearby(cu1_1, 5);
                                if(cu5 != null){
                                    if(cu5.getLevelCopy4Id() == 6){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu5, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu5, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu5.getLevelCopy4Id() == 7){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu5, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu1_1.getLevelCopy4Id() == 6){
                                BigDecimal bd = new BigDecimal(67);
                                //if(orderLevel == 2){
                                    //bd = new BigDecimal(52);
                                //}else if(orderLevel == 3){
                                    //bd = new BigDecimal(37);
                                //}else if(orderLevel == 4){
                                    //bd = new BigDecimal(22);
                                //}else if(orderLevel == 5){
                                    //bd = new BigDecimal(11);
                                //}
                                handleAccount1(cu1_1.getId(), bd, orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为6的
                                Customer nearby6 = findCustomer_nearby(cu1_1, 6);
                                if(nearby6 != null){
                                    handleAccount1(nearby6.getId(), bd.multiply(BigDecimal.valueOf(0.2)), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于6的
                                Customer cu6 = findParentUser_nearby(cu1_1, 6);
                                if(cu6 != null){
                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    // 找一个就近为7的
                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                    if(nearby7 != null){
                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    }
                                }
                            }else if(cu1_1.getLevelCopy4Id() == 7){
                                BigDecimal bd = new BigDecimal(78);
                                //if(orderLevel == 2){
                                    //bd = new BigDecimal(63);
                                //}else if(orderLevel == 3){
                                    //bd = new BigDecimal(48);
                                //}else if(orderLevel == 4){
                                    //bd = new BigDecimal(33);
                                //}else if(orderLevel == 5){
                                    //bd = new BigDecimal(22);
                                //}else if(orderLevel == 6){
                                    //bd = new BigDecimal(11);
                                //}
                                handleAccount1(cu1_1.getId(), bd, orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为7的
                                Customer nearby7 = findCustomer_nearby(cu1_1, 7);
                                if(nearby7 != null){
                                    handleAccount1(nearby7.getId(), bd.multiply(BigDecimal.valueOf(0.2)), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                            }
                        }
                    }else if(orderLevel == 2){
                        // 继续找大于2的
                        Customer cu2 = findParentUser_nearby(customer, 2);
                        if(cu2 != null){
                            if(cu2.getLevelCopy4Id() == 3){
                                handleAccount1(cu2.getId(), BigDecimal.valueOf(15), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为3的
                                Customer nearby3 = findCustomer_nearby(cu2, 3);
                                if(nearby3 != null){
                                    handleAccount1(nearby3.getId(), BigDecimal.valueOf(15 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于3的
                                Customer cu3 = findParentUser_nearby(cu2, 3);
                                if(cu3 != null){
                                    if(cu3.getLevelCopy4Id() == 4){
                                        handleAccount1(cu3.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为4的
                                        Customer nearby4 = findCustomer_nearby(cu3, 4);
                                        if(nearby4 != null){
                                            handleAccount1(nearby4.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于4的
                                        Customer cu4 = findParentUser_nearby(cu3, 4);
                                        if(cu4 != null){
                                            if(cu4.getLevelCopy4Id() == 5){
                                                handleAccount1(cu4.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为5的
                                                Customer nearby5 = findCustomer_nearby(cu4, 5);
                                                if(nearby5 != null){
                                                    handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于5的
                                                Customer cu5 = findParentUser_nearby(cu4, 5);
                                                if(cu5 != null){
                                                    if(cu5.getLevelCopy4Id() == 6){
                                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        // 找一个就近为6的
                                                        Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                        if(nearby6 != null){
                                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        }
                                                        // 继续找大于6的
                                                        Customer cu6 = findParentUser_nearby(cu5, 6);
                                                        if(cu6 != null){
                                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                            // 找一个就近为7的
                                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                            if(nearby7 != null){
                                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                            }
                                                        }
                                                    }else if(cu5.getLevelCopy4Id() == 7){
                                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        // 找一个就近为7的
                                                        Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                        if(nearby7 != null){
                                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                        }
                                                    }
                                                }
                                            }else if(cu4.getLevelCopy4Id() == 6){
                                                handleAccount1(cu4.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu4, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu4, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu4.getLevelCopy4Id() == 7){
                                                handleAccount1(cu4.getId(), BigDecimal.valueOf(33), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu4, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(33 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu3.getLevelCopy4Id() == 5){
                                        handleAccount1(cu3.getId(), BigDecimal.valueOf(26), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为5的
                                        Customer nearby5 = findCustomer_nearby(cu3, 5);
                                        if(nearby5 != null){
                                            handleAccount1(nearby5.getId(), BigDecimal.valueOf(26 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于5的
                                        Customer cu5 = findParentUser_nearby(cu3, 5);
                                        if(cu5 != null){
                                            if(cu5.getLevelCopy4Id() == 6){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu5, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu5.getLevelCopy4Id() == 7){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu3.getLevelCopy4Id() == 6){
                                        handleAccount1(cu3.getId(), BigDecimal.valueOf(37), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu3, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(37 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu3, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu3.getLevelCopy4Id() == 7){
                                        handleAccount1(cu3.getId(), BigDecimal.valueOf(48), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu3, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(48 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu2.getLevelCopy4Id() == 4){
                                handleAccount1(cu2.getId(), BigDecimal.valueOf(30), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为4的
                                Customer nearby4 = findCustomer_nearby(cu2, 4);
                                if(nearby4 != null){
                                    handleAccount1(nearby4.getId(), BigDecimal.valueOf(30 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于4的
                                Customer cu4 = findParentUser_nearby(cu2, 4);
                                if(cu4 != null){
                                    if(cu4.getLevelCopy4Id() == 5){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为5的
                                        Customer nearby5 = findCustomer_nearby(cu4, 5);
                                        if(nearby5 != null){
                                            handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于5的
                                        Customer cu5 = findParentUser_nearby(cu4, 5);
                                        if(cu5 != null){
                                            if(cu5.getLevelCopy4Id() == 6){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu5, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu5.getLevelCopy4Id() == 7){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu4.getLevelCopy4Id() == 6){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu4, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu4, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu4.getLevelCopy4Id() == 7){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(33), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu4, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(33 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu2.getLevelCopy4Id() == 5){
                                handleAccount1(cu2.getId(), BigDecimal.valueOf(41), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为5的
                                Customer nearby5 = findCustomer_nearby(cu2, 5);
                                if(nearby5 != null){
                                    handleAccount1(nearby5.getId(), BigDecimal.valueOf(41 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于5的
                                Customer cu5 = findParentUser_nearby(cu2, 5);
                                if(cu5 != null){
                                    if(cu5.getLevelCopy4Id() == 6){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu5, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu5, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu5.getLevelCopy4Id() == 7){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu5, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu2.getLevelCopy4Id() == 6){
                                handleAccount1(cu2.getId(), BigDecimal.valueOf(52), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为6的
                                Customer nearby6 = findCustomer_nearby(cu2, 6);
                                if(nearby6 != null){
                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(52 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于6的
                                Customer cu6 = findParentUser_nearby(cu2, 6);
                                if(cu6 != null){
                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    // 找一个就近为7的
                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                    if(nearby7 != null){
                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    }
                                }
                            }else if(cu2.getLevelCopy4Id() == 7){
                                handleAccount1(cu2.getId(), BigDecimal.valueOf(63), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为7的
                                Customer nearby7 = findCustomer_nearby(cu2, 7);
                                if(nearby7 != null){
                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(63 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                            }
                        }
                    }else if(orderLevel == 3){
                        // 继续找大于3的
                        Customer cu3 = findParentUser_nearby(customer, 3);
                        if(cu3 != null){
                            if(cu3.getLevelCopy4Id() == 4){
                                handleAccount1(cu3.getId(), BigDecimal.valueOf(15), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为4的
                                Customer nearby4 = findCustomer_nearby(cu3, 4);
                                if(nearby4 != null){
                                    handleAccount1(nearby4.getId(), BigDecimal.valueOf(15 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于4的
                                Customer cu4 = findParentUser_nearby(cu3, 4);
                                if(cu4 != null){
                                    if(cu4.getLevelCopy4Id() == 5){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为5的
                                        Customer nearby5 = findCustomer_nearby(cu4, 5);
                                        if(nearby5 != null){
                                            handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于5的
                                        Customer cu5 = findParentUser_nearby(cu4, 5);
                                        if(cu5 != null){
                                            if(cu5.getLevelCopy4Id() == 6){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为6的
                                                Customer nearby6 = findCustomer_nearby(cu5, 6);
                                                if(nearby6 != null){
                                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                                // 继续找大于6的
                                                Customer cu6 = findParentUser_nearby(cu5, 6);
                                                if(cu6 != null){
                                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    // 找一个就近为7的
                                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                                    if(nearby7 != null){
                                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                    }
                                                }
                                            }else if(cu5.getLevelCopy4Id() == 7){
                                                handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                // 找一个就近为7的
                                                Customer nearby7 = findCustomer_nearby(cu5, 7);
                                                if(nearby7 != null){
                                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                                }
                                            }
                                        }
                                    }else if(cu4.getLevelCopy4Id() == 6){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu4, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu4, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu4.getLevelCopy4Id() == 7){
                                        handleAccount1(cu4.getId(), BigDecimal.valueOf(33), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu4, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(33 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu3.getLevelCopy4Id() == 5){
                                handleAccount1(cu3.getId(), BigDecimal.valueOf(26), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为5的
                                Customer nearby5 = findCustomer_nearby(cu3, 5);
                                if(nearby5 != null){
                                    handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于5的
                                Customer cu5 = findParentUser_nearby(cu3, 5);
                                if(cu5 != null){
                                    if(cu5.getLevelCopy4Id() == 6){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu5, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu5, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu5.getLevelCopy4Id() == 7){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu5, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu3.getLevelCopy4Id() == 6){
                                handleAccount1(cu3.getId(), BigDecimal.valueOf(37), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为6的
                                Customer nearby6 = findCustomer_nearby(cu3, 6);
                                if(nearby6 != null){
                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(37 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于6的
                                Customer cu6 = findParentUser_nearby(cu3, 6);
                                if(cu6 != null){
                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    // 找一个就近为7的
                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                    if(nearby7 != null){
                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    }
                                }
                            }else if(cu3.getLevelCopy4Id() == 7){
                                handleAccount1(cu3.getId(), BigDecimal.valueOf(48), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为7的
                                Customer nearby7 = findCustomer_nearby(cu3, 7);
                                if(nearby7 != null){
                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(48 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                            }
                        }
                    }else if(orderLevel == 4){
                        // 继续找大于4的
                        Customer cu4 = findParentUser_nearby(customer, 4);
                        if(cu4 != null){
                            if(cu4.getLevelCopy4Id() == 5){
                                handleAccount1(cu4.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为5的
                                Customer nearby5 = findCustomer_nearby(cu4, 5);
                                if(nearby5 != null){
                                    handleAccount1(nearby5.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于5的
                                Customer cu5 = findParentUser_nearby(cu4, 5);
                                if(cu5 != null){
                                    if(cu5.getLevelCopy4Id() == 6){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为6的
                                        Customer nearby6 = findCustomer_nearby(cu5, 6);
                                        if(nearby6 != null){
                                            handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                        // 继续找大于6的
                                        Customer cu6 = findParentUser_nearby(cu5, 6);
                                        if(cu6 != null){
                                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            // 找一个就近为7的
                                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                                            if(nearby7 != null){
                                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                            }
                                        }
                                    }else if(cu5.getLevelCopy4Id() == 7){
                                        handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        // 找一个就近为7的
                                        Customer nearby7 = findCustomer_nearby(cu5, 7);
                                        if(nearby7 != null){
                                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                        }
                                    }
                                }
                            }else if(cu4.getLevelCopy4Id() == 6){
                                handleAccount1(cu4.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为6的
                                Customer nearby6 = findCustomer_nearby(cu4, 6);
                                if(nearby6 != null){
                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于6的
                                Customer cu6 = findParentUser_nearby(cu4, 6);
                                if(cu6 != null){
                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    // 找一个就近为7的
                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                    if(nearby7 != null){
                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    }
                                }
                            }else if(cu4.getLevelCopy4Id() == 7){
                                handleAccount1(cu4.getId(), BigDecimal.valueOf(33), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为7的
                                Customer nearby7 = findCustomer_nearby(cu4, 7);
                                if(nearby7 != null){
                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(33 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                            }
                        }
                    }else if(orderLevel == 5){
                        // 继续找大于5的
                        Customer cu5 = findParentUser_nearby(customer, 5);
                        if(cu5 != null){
                            if(cu5.getLevelCopy4Id() == 6){
                                handleAccount1(cu5.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为6的
                                Customer nearby6 = findCustomer_nearby(cu5, 6);
                                if(nearby6 != null){
                                    handleAccount1(nearby6.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                                // 继续找大于6的
                                Customer cu6 = findParentUser_nearby(cu5, 6);
                                if(cu6 != null){
                                    handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    // 找一个就近为7的
                                    Customer nearby7 = findCustomer_nearby(cu6, 7);
                                    if(nearby7 != null){
                                        handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                    }
                                }
                            }else if(cu5.getLevelCopy4Id() == 7){
                                handleAccount1(cu5.getId(), BigDecimal.valueOf(22), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                // 找一个就近为7的
                                Customer nearby7 = findCustomer_nearby(cu5, 7);
                                if(nearby7 != null){
                                    handleAccount1(nearby7.getId(), BigDecimal.valueOf(22 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                                }
                            }
                        }
                    }else if(orderLevel == 6){
                        // 继续找大于6的
                        Customer cu6 = findParentUser_nearby(customer, 6);
                        if(cu6 != null){
                            handleAccount1(cu6.getId(), BigDecimal.valueOf(11), orders.getOrderNo(), customer.getAgentCode(), quantity);
                            // 找一个就近为7的
                            Customer nearby7 = findCustomer_nearby(cu6, 7);
                            if(nearby7 != null){
                                handleAccount1(nearby7.getId(), BigDecimal.valueOf(11 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                            }
                        }
                    }else if(orderLevel == 7){
                        // 找一个就近为7的
                        Customer nearby7 = findCustomer_nearby(customer, 7);
                        if(nearby7 != null){
                            handleAccount1(nearby7.getId(), BigDecimal.valueOf(78 * 0.2), orders.getOrderNo(), customer.getAgentCode(), quantity);
                        }
                    }
                }
            }
        }
    }


    /**
     * 获取就近高等级
     * @param customer
     * @param levelCopy4Id
     * @return
     */
    public Customer findParentUser_nearby(Customer customer, Integer levelCopy4Id){
        Integer newId = 0;
        if (customer.getParentId() != null && customer.getParentId() != 1198 && customer.getLevelCopy4Id() != 8) {
            newId = customer.getParentId();
        }
        Customer newCustomer = customerDao.queryById(newId);
        if(newCustomer == null){
            return newCustomer;
        }
        if (newCustomer.getLevelCopy4Id() > levelCopy4Id) {
            return newCustomer;
        } else {
            while (newCustomer.getLevelCopy4Id() <= levelCopy4Id) {
                newCustomer = customerDao.queryById(newId);
                if(newCustomer == null){
                    break;
                }
                if (newCustomer.getLevelCopy4Id() > levelCopy4Id) {
                    break;
                } else {
                    if (newCustomer.getParentId() != null && newCustomer.getParentId() != 1198 && newCustomer.getLevelCopy4Id() != 8) {
                        newId = newCustomer.getParentId();
                    }else{
                        break;
                    }
                }
            }
        }
        return newCustomer;
    }


    /**
     * 获取就近同等级
     * @param customer
     * @param levelCopy4Id
     * @return
     */
    public Customer findCustomer_nearby(Customer customer, Integer levelCopy4Id){
        Integer newId = 0;
        if (customer.getParentId() != null && customer.getParentId() != 1198 && customer.getLevelCopy4Id() != 8) {
            newId = customer.getParentId();
        }
        Customer newCustomer = customerDao.queryById(newId);
        if(newCustomer == null){
            return newCustomer;
        }
        if (newCustomer.getLevelCopy4Id() == levelCopy4Id) {
            return newCustomer;
        } else {
            while (newCustomer.getLevelCopy4Id() != levelCopy4Id) {
                newCustomer = customerDao.queryById(newId);
                if(newCustomer == null){
                    break;
                }
                if(newId == 1 || newCustomer.getLevelCopy4Id() == 8){
                    return null;
                }
                if (newCustomer.getLevelCopy4Id() == levelCopy4Id) {
                    break;
                } else {
                    if (newCustomer.getParentId() != null && newCustomer.getParentId() != 1198 && newCustomer.getLevelCopy4Id() != 8) {
                        newId = newCustomer.getParentId();
                    }else{
                        break;
                    }
                }
            }
        }
        return newCustomer;
    }


    /**
     * 共享模式 - 增加业绩记录
     * @param userId
     * @param bd
     * @param orderNo
     * @param agentCode
     */
    public void handleAccount2(Integer userId, BigDecimal amount, BigDecimal bd, String orderNo, String agentCode){
        AccountRecord ar1 = new AccountRecord();
        ar1.setIntoUserId(userId);
        ar1.setUpdateTime(new Date());
        ar1.setType("4");
        ar1.setAmount(amount);
        ar1.setNowAmount(bd);
        ar1.setSign("1");
        ar1.setOrderNo(orderNo);
        ar1.setInfo(agentCode + "购买成功! 您的业绩增加：" + amount);
        accountRecordDao.save(ar1);
    }


    /**
     * 共享模式 - 增加共享余额记录
     * @param userId
     * @param amount
     * @param orderNo
     * @param agentCode
     * @param quantity
     */
    public void handleAccount1(Integer userId, BigDecimal amount, String orderNo, String agentCode, Integer quantity){
        Account account = accountDao.queryAccountByUserId(userId);
        BigDecimal shareBalance = amount.multiply(BigDecimal.valueOf(0.7)).multiply(BigDecimal.valueOf(quantity));
        BigDecimal frozenIn = amount.multiply(BigDecimal.valueOf(0.3)).multiply(BigDecimal.valueOf(quantity));
        account.setShareBalance(account.getShareBalance().add(shareBalance));
        account.setFrozenIn(account.getFrozenIn().add(frozenIn));
        accountDao.update(account);
        AccountRecord ar = new AccountRecord();
        ar.setIntoUserId(userId);
        ar.setUpdateTime(new Date());
        ar.setType("10");
        ar.setAmount(frozenIn);
        ar.setNowAmount(account.getFrozenIn());
        ar.setSign("1");
        ar.setOrderNo(orderNo);
        ar.setInfo(agentCode + "购买成功! 您获得管理奖（冻结）" + frozenIn);
        accountRecordDao.save(ar);
        AccountRecord ar1 = new AccountRecord();
        ar1.setIntoUserId(userId);
        ar1.setUpdateTime(new Date());
        ar1.setType("10");
        ar1.setAmount(shareBalance);
        ar1.setNowAmount(account.getShareBalance());
        ar1.setSign("1");
        ar1.setOrderNo(orderNo);
        ar1.setInfo(agentCode + "购买成功! 您获得管理奖：" + shareBalance);
        accountRecordDao.save(ar1);
        if(account.getFrozenIn().doubleValue() >= 369){
            // 进入复消
            OrdersShareRepeat osr = new OrdersShareRepeat();
            osr.setUserId(account.getUserId());
            osr.setUpdateTime(new Date());
            osr.setStatus("0");
            ordersShareRepeatDao.save(osr);
        }
    }


    /**
     * 获取签到数据
     * @param request
     */
    @GetMapping("/getSignInData")
    public ResponseServer getSignInData(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String strTime = sdf.format(new Date());
        List<SignIn> signInList = signInDao.queryByUserIdAndMonth(customer.getId(), strTime);
        Map map = new HashMap();
        List list = new ArrayList();
        if(signInList.size() > 0){
            for(SignIn si : signInList){
                SignInDTO dto = new SignInDTO();
                dto.setDay(sdf1.format(si.getSignInTime()));
                dto.setStatus("yes");
                list.add(dto);
            }
        }
        map.put("this_month_list", list);
//        SignInCopy signInCopy = signInCopyDao.queryByUserId(customer.getId());
//        if(signInCopy == null){
//            map.put("continuity_day", 0);
//        }else{
//            Calendar now = Calendar.getInstance();
//            now.setTime(new Date());
//            now.add(Calendar.DAY_OF_MONTH, -1);
//            String date1 = sdf1.format(now.getTime());
//            SignIn signIn_1 = signInDao.queryByTodayIsSignIn(customer.getId(), date1);
//            if(signIn_1 == null){
//                map.put("continuity_day", 0);
//            }else{
//                map.put("continuity_day", signInCopy.getContinuity());
//            }
//            signInCopyDao.update(signInCopy);
//        }
        map.put("continuity_day", 0);
        map.put("isShow", "0");
        return ResponseServer.success("加载成功", map);
    }


    /**
     * 点击签到
     * @param request
     */
    @PostMapping("/clickSignIn")
    public ResponseServer clickSignIn(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(date);
        SignIn signIn = signInDao.queryByTodayIsSignIn(customer.getId(), time);
        if(signIn == null) {
            SignIn newSi = new SignIn();
            newSi.setUserId(customer.getId());
            newSi.setSignInTime(date);
            signInDao.save(newSi);

            // 送9个极乐值，30张100专区参团券，30张200专区参团券
//            Account acc = accountDao.queryAccountByUserId(customer.getId());
//            acc.setBliss(acc.getBliss() + 9);
//            accountDao.update(acc);
//            AccountRecord ar = new AccountRecord();
//            ar.setIntoUserId(customer.getId());
//            ar.setType("2");
//            ar.setSign("1");
//            ar.setUpdateTime(date);
//            ar.setAmount(new BigDecimal(9));
//            ar.setNowAmount(new BigDecimal(acc.getBliss()));
//            ar.setInfo(customer.getAgentCode() + "签到成功! 获极乐值：" + 9);
//            accountRecordDao.save(ar);

            UserCoupon uc3 = userCouponDao.queryByUserIdAndCouponId(customer.getId(), 3);
            if(uc3 == null){
                UserCoupon newUc = new UserCoupon();
                newUc.setCouponId(3);
                newUc.setQuantity(15); // 固定30张
                newUc.setUserId(customer.getId());
                newUc.setUpdateTime(date);
                userCouponDao.save(newUc);
            }else{
                uc3.setQuantity(15); // 固定30张
                userCouponDao.update(uc3);
            }
            AccountRecord ar3 = new AccountRecord();
            ar3.setIntoUserId(customer.getId());
            ar3.setType("7");
            ar3.setSign("1");
            ar3.setUpdateTime(date);
            ar3.setAmount(new BigDecimal(15));
            ar3.setNowAmount(new BigDecimal(15));
            ar3.setInfo(customer.getAgentCode() + "签到成功! 获100专区参团券：" + 15);
            accountRecordDao.save(ar3);

            UserCoupon uc4 = userCouponDao.queryByUserIdAndCouponId(customer.getId(), 4);
            if(uc4 == null){
                UserCoupon newUc = new UserCoupon();
                newUc.setCouponId(4);
                newUc.setQuantity(15); // 固定30张
                newUc.setUserId(customer.getId());
                newUc.setUpdateTime(date);
                userCouponDao.save(newUc);
            }else{
                uc4.setQuantity(15); // 固定30张
                userCouponDao.update(uc4);
            }
            AccountRecord ar4 = new AccountRecord();
            ar4.setIntoUserId(customer.getId());
            ar4.setType("9");
            ar4.setSign("1");
            ar4.setUpdateTime(date);
            ar4.setAmount(new BigDecimal(15));
            ar4.setNowAmount(new BigDecimal(15));
            ar4.setInfo(customer.getAgentCode() + "签到成功! 获200专区参团券：" + 15);
            accountRecordDao.save(ar4);

            SignInCopy signInCopy = signInCopyDao.queryByUserId(customer.getId());
            if(signInCopy == null){
                SignInCopy copy = new SignInCopy();
                copy.setContinuity(1);
                copy.setUserId(customer.getId());
                signInCopyDao.save(copy);
            }else{
                Calendar now = Calendar.getInstance();
                now.setTime(date);
                now.add(Calendar.DAY_OF_MONTH, -1);
                String date1 = sdf.format(now.getTime());
                SignIn signIn_1 = signInDao.queryByTodayIsSignIn(customer.getId(), date1);
                if(signIn_1 == null){
                    signInCopy.setContinuity(1);
                }else{
                    signInCopy.setContinuity(signInCopy.getContinuity() + 1);
                }
                signInCopyDao.update(signInCopy);
            }

            return ResponseServer.success("签到成功!");
        }
        return ResponseServer.error("今日已签到!");
    }


    /**
     * 获取银行卡数据
     * @param request
     * @return
     */
    @GetMapping("/bankCardData")
    public ResponseServer bankCardData(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        List<UserCardDTO> dtoList = userCardDao.queryByUserId(customer.getId());
        if(dtoList.size() > 0){
            for(int i = 0; i < dtoList.size(); i++){
                UserCardDTO dto = dtoList.get(i);
                if(i == 0){
                    dto.setSelected(1);
                }else{
                    dto.setSelected(0);
                }
            }
        }
        return ResponseServer.success(dtoList);
    }


    /**
     * 添加银行卡信息
     * @param request
     * @param parameter
     * @return
     */
    @PostMapping("/createBankCard")
    public ResponseServer createBankCard(@RequestBody String parameter, HttpServletRequest request){
        System.err.println("添加银行卡信息********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String name = jsonObject.get("name").toString();
        String cardType = jsonObject.get("cardType").toString();
        String phoneNumber = jsonObject.get("phoneNumber").toString();
        String bankName = jsonObject.get("bankName").toString();
        String bankNumber = jsonObject.get("bankNumber").toString();
        UserCard uc = userCardDao.queryByBankNumber(bankNumber);
        if(uc == null){
            UserCard userCard = new UserCard();
            userCard.setUserId(customer.getId());
            userCard.setHolder(name);
            userCard.setBankName(cardType);
            userCard.setCardNumber(bankNumber);
            userCard.setPhone(phoneNumber);
            userCard.setTypeName(bankName);
            userCard.setUpdateTime(new Date());
            userCardDao.save(userCard);
            return ResponseServer.success("添加银行卡成功");
        }else{
            return ResponseServer.error("该银行卡已存在!");
        }
    }


    /**
     * 删除银行卡信息
     * @param parameter
     * @return
     */
    @PostMapping("/deleteBankCard")
    public ResponseServer deleteBankCard(@RequestBody String parameter){
        System.err.println("删除银行卡信息********** parameter ---------- : " + parameter);
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        Integer cardId = (Integer) jsonObject.get("cardId");
        userCardDao.delete(cardId);
        return ResponseServer.success("删除银行卡成功");
    }


    /**
     * 个人提现
     * @param request
     * @param parameter  [type] 1=余额提现，2=拼团余额提现，3=共享余额
     * @return
     */
    @PostMapping("/cardWithdrawalData")
    public ResponseServer cardWithdrawalData(@RequestBody String parameter, HttpServletRequest request){
        System.err.println("个人提现********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        Integer cardId = (Integer) jsonObject.get("cardId");
        BigDecimal amount = (BigDecimal) jsonObject.get("amount");
        String type = jsonObject.get("type").toString();

        if(amount == null){
            return ResponseServer.error("提现金额需大于等于100元!");
        }else{
            if(amount.doubleValue() < 100){
                return ResponseServer.error("提现金额需大于等于100元!");
            }else{
                if(amount.doubleValue() % 100 != 0){
                    return ResponseServer.error("提现金额需为100的倍数!");
                }
            }
        }
        UserCard userCard = userCardDao.queryById(cardId);
        if(userCard == null){
            return ResponseServer.error("银行卡信息不正确!");
        }
        // 提现订单号
        String orderNo = "TXH" + Units.getOrderIdByTime().replaceAll("-", "");
        // 账户
        Account account = accountDao.queryAccountByUserId(customer.getId());
        // 手续费
        BigDecimal bigD = null;
        if("1".equals(type)) {
            // 余额
            if (account.getVoucherBalance().doubleValue() < amount.doubleValue()) {
                return ResponseServer.error("余额不足");
            }
            if (amount.doubleValue() <= 5000) {
                bigD = new BigDecimal(0.03); // 5000以下
            } else if (amount.doubleValue() > 5000) {
                bigD = new BigDecimal(0.02); // 5000以上
            }
        }else if("2".equals(type)){
            // 拼团余额
            if(account.getCollageBalance().doubleValue() < amount.doubleValue()){
                return ResponseServer.error("拼团余额不足");
            }
            if("0".equals(customer.getIsMember())){
                bigD = new BigDecimal(0.06); // 非会员
            }else if(amount.doubleValue() > 5000){
                bigD = new BigDecimal(0.05); // 会员
            }
        }else{
            // 共享余额
        }
        // 扣除的手续费
        BigDecimal deduction = amount.multiply(bigD);
        // 实际到账的钱
        BigDecimal remain = amount.subtract(deduction);
        String aType = "";
        BigDecimal nowAmount = null;
        if("1".equals(type)) {
            // 余额
            account.setVoucherBalance(account.getVoucherBalance().subtract(amount));
            accountDao.update(account);
            aType = "0";
            nowAmount = account.getVoucherBalance();
        }else if("2".equals(type)){
            // 拼团余额
            account.setVoucherBalance(account.getCollageBalance().subtract(amount));
            accountDao.update(account);
            aType = "8";
            nowAmount = account.getCollageBalance();
        }else{
            // 共享余额

        }
        // 账户记录
        AccountRecord ar = new AccountRecord();
        ar.setIntoUserId(customer.getId());
        ar.setUpdateTime(new Date());
        ar.setType(aType);
        ar.setAmount(amount);
        ar.setNowAmount(nowAmount);
        ar.setSign("2");
        ar.setInfo(customer.getAgentCode() + "提现金额为：" + amount);
        ar.setOrderNo(orderNo);
        accountRecordDao.save(ar);
        // 提现信息
        CardWithdrawal cw = new CardWithdrawal();
        cw.setOrderNo(orderNo);
        cw.setAmount(amount);
        cw.setChargeFee(bigD);
        cw.setNowAmount(remain);
        cw.setUserId(customer.getId());
        cw.setUserCardId(cardId);
        cw.setStatus("0");
        cw.setCreateTime(new Date());
        cw.setBankName(userCard.getBankName());
        cw.setTypeName(userCard.getTypeName());
        cw.setCardNumber(userCard.getTypeName() + "：" + userCard.getCardNumber());
        cw.setHolder(userCard.getHolder());
        cw.setPhone(userCard.getPhone());
        cw.setAccountRecordId(ar.getId());
        cw.setIdCard(customer.getIdCard() + " : ");
        cardWithdrawalDao.save(cw);
        return ResponseServer.success("提现成功!");
    }


    /**
     * 修改头像
     * @param parameter
     * @param request
     * @return
     */
    @PostMapping("/updateHeadImgData")
    public ResponseServer updateHeadImgData(@RequestBody String parameter, HttpServletRequest request){
        System.err.println("修改头像********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        if(jsonObject.get("nickName") == null){

        }else{
            customer.setNickName(jsonObject.get("nickName").toString());
        }
        if(jsonObject.get("headImg") == null) {

        }else{
            customer.setHeadImg(jsonObject.get("headImg").toString());
        }
        customerDao.update(customer);
        return ResponseServer.success("修改资料成功");
    }


    /**
     * 提交实名认证数据
     * @param parameter
     * @param request
     * @return
     */
    @PostMapping("/submitRealNameAuth")
    public ResponseServer submitRealNameAuth(@RequestBody String parameter, HttpServletRequest request){
        System.err.println("提交实名认证数据********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        String cardPositive = jsonObject.get("cardPositive").toString(); //正面图
        String cardBack = jsonObject.get("cardBack").toString(); //反面图
        String userName = jsonObject.get("userName").toString(); //姓名
        String idCard = jsonObject.get("idCard").toString(); //身份证号
        customer.setRealName(userName);
        customer.setIdCard(idCard);
        customer.setCardPositive(cardPositive);
        customer.setCardBack(cardBack);
        customer.setVerifyStatus("0");
        if(!"".equals(customer.getFailedInfo())){
            customer.setFailedInfo("");
        }
        customerDao.update(customer);
        AppRealNameAuthVO vo = new AppRealNameAuthVO();
        vo.setUserId(customer.getId());
        vo.setCardPositive(customer.getCardPositive());
        vo.setCardBack(customer.getCardBack());
        vo.setVerifyStatus(customer.getVerifyStatus());
        vo.setIdCard(customer.getIdCard());
        vo.setUserName(customer.getRealName());
        vo.setFailedInfo(customer.getFailedInfo());
        return ResponseServer.success("申请成功", vo);
    }


    /**
     * 查看实名认证的数据
     * @param request
     * @return
     */
    @GetMapping("/getRealNameAuthData")
    public ResponseServer getRealNameAuthData(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        AppRealNameAuthVO vo = new AppRealNameAuthVO();
        vo.setUserId(customer.getId());
        vo.setCardPositive(customer.getCardPositive());
        vo.setCardBack(customer.getCardBack());
        vo.setVerifyStatus(customer.getVerifyStatus());
        vo.setIdCard(customer.getIdCard());
        vo.setUserName(customer.getRealName());
        vo.setFailedInfo(customer.getFailedInfo());
        return ResponseServer.success("加载成功", vo);
    }


    /**
     * 获取通讯录(邀请记录)数据
     * @param request
     * @return
     */
    @GetMapping("/getMailListData")
    public ResponseServer getMailListData(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        Map map = new HashMap();
        if(customer.getParentId() != null && customer.getParentId() != 1198){
            List<AppMailListVO> parentList = customerDao.queryByMailList_parent(customer.getParentId());
            map.put("parentData", parentList);
        }else{
            map.put("parentData", null);
        }
        List<AppMailListVO> voList = customerDao.queryByMailList(customer.getId());
        map.put("mailListData", voList);
        //if(voList.size() > 0){
            //int i = voList.size();
            //for(AppMailListVO vo: voList){
                // i += customerService.queryTeamTotalNumber(vo.getUserId());
                // i += customerDao.queryTeamBySign(vo.getUserId());
            //}
            //map.put("teamTotalNumber", i);
        //}else{
            //map.put("teamTotalNumber", 0);
        //}
        map.put("teamTotalNumber", customer.getSign());
        return ResponseServer.success("加载成功", map);
    }


    /**
     * 通讯录 - 根据编号搜索用户
     * @param request
     * @return
     */
    @GetMapping("/queryCustomerByAgentCode")
    public ResponseServer queryCustomerByAgentCode(HttpServletRequest request, String agentCode){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        List<AppMailListVO> voList = customerDao.queryCustomerByAgentCode_1(agentCode);
        return ResponseServer.success("成功", voList);
    }


    /**
     * 获取我的账户(钱包)数据
     * @param request
     * @return
     */
    @GetMapping("/getAccountData")
    public ResponseServer getAccountData(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        Account account = accountDao.queryAccountByUserId(customer.getId());
        if(account == null){
            return ResponseServer.success("账户信息加载失败, 请联系管理员");
        }
        AppAccountDTO dto = new AppAccountDTO();
        dto.setId(account.getId());
        dto.setUserId(account.getUserId());
        dto.setVoucherBalance(account.getVoucherBalance());
        dto.setIntegral(account.getIntegral());
        dto.setBliss(account.getBliss());
        dto.setRedPacket(account.getRedPacket());

        BigDecimal dj = todayCollageProfitDao.queryByUserIdAndStatus(account.getUserId());
        dto.setCollageBalance(account.getCollageBalance().add(dj));


        dto.setShareBalance(account.getShareBalance());
        dto.setFrozenInShare(account.getFrozenIn());

        //dto.setFrozenInCollage(todayCollageVoucherDao.queryByTotalAmount(account.getUserId()));
        BigDecimal b = collageTotalVoucherDao.queryByTotalAmount(account.getUserId());
        BigDecimal b1 = todayCollageVoucherDao.queryByTotalAmount_1(account.getUserId());
        dto.setFrozenInCollage(b.add(b1));


        List<AppAccountTypeDTO> dtoList = new ArrayList<>();
        // 0=余额，1=积分，2=极乐值，3=红包券，4=业绩，5=拼团券，6=共享券，7=100元专区参团券，8=拼团余额，9=200元专区参团券，10=共享余额
        AppAccountTypeDTO type100 = new AppAccountTypeDTO();
        type100.setType("100");
        type100.setTypeName("全部");
        type100.setIsSelect("1");
        dtoList.add(type100);
        AppAccountTypeDTO type0 = new AppAccountTypeDTO();
        type0.setType("0");
        type0.setTypeName("余额");
        type0.setIsSelect("0");
        dtoList.add(type0);
//        AppAccountTypeDTO type1 = new AppAccountTypeDTO();
//        type1.setType("1");
//        type1.setTypeName("积分");
//        type1.setIsSelect("0");
//        dtoList.add(type1);
//        AppAccountTypeDTO type2 = new AppAccountTypeDTO();
//        type2.setType("2");
//        type2.setTypeName("极乐值");
//        type2.setIsSelect("0");
//        dtoList.add(type2);
//        AppAccountTypeDTO type3 = new AppAccountTypeDTO();
//        type3.setType("3");
//        type3.setTypeName("红包券");
//        type3.setIsSelect("0");
//        dtoList.add(type3);
        AppAccountTypeDTO type4 = new AppAccountTypeDTO();
        type4.setType("4");
        type4.setTypeName("业绩");
        type4.setIsSelect("0");
        dtoList.add(type4);
//        AppAccountTypeDTO type5 = new AppAccountTypeDTO();
//        type5.setType("5");
//        type5.setTypeName("拼团券");
//        type5.setIsSelect("0");
//        dtoList.add(type5);
//        AppAccountTypeDTO type6 = new AppAccountTypeDTO();
//        type6.setType("6");
//        type6.setTypeName("共享券");
//        type6.setIsSelect("0");
//        dtoList.add(type6);
        //AppAccountTypeDTO type7 = new AppAccountTypeDTO();
        //type7.setType("7");
        //type7.setTypeName("100参团券");
        //type7.setIsSelect("0");
        //dtoList.add(type7);
        AppAccountTypeDTO type8 = new AppAccountTypeDTO();
        type8.setType("8");
        type8.setTypeName("拼团余额");
        type8.setIsSelect("0");
        dtoList.add(type8);
        //AppAccountTypeDTO type9 = new AppAccountTypeDTO();
        //type9.setType("9");
        //type9.setTypeName("200参团券");
        //type9.setIsSelect("0");
        //dtoList.add(type9);
        AppAccountTypeDTO type10 = new AppAccountTypeDTO();
        type10.setType("10");
        type10.setTypeName("共享余额");
        type10.setIsSelect("0");
        dtoList.add(type10);
        dto.setAccountTypList(dtoList);
        return ResponseServer.success("加载成功", dto);
    }


    /**
     * 查看物流数据
     * @param logisticsNumber
     * @return
     */
    @GetMapping("/getLogisticsData")
    public ResponseServer getLogisticsData(String logisticsNumber) throws Exception {
        String json = CreateUnifiedOrder.getWuliu(logisticsNumber);
        JSONObject jsonObject = JSONObject.parseObject(json);
        String result = jsonObject.get("result").toString();
        JSONObject jsonObject1 = JSONObject.parseObject(result);
        String number = jsonObject1.get("number").toString();
        String type = jsonObject1.get("type").toString();
        String list = jsonObject1.get("list").toString();
        JSONArray jsonArray = JSONArray.parseArray(list);
        List<LogisticsDTO_1> dto1List = new ArrayList<>();
        if(jsonArray.size() > 0){
            for(int i = 0; i < jsonArray.size(); i++){
                JSONObject object = (JSONObject) jsonArray.get(i);
                String time = object.get("time").toString();
                String status = object.get("status").toString();
                LogisticsDTO_1 dto1 = new LogisticsDTO_1();
                dto1.setTime(time);
                dto1.setStatus(status);
                dto1List.add(dto1);
            }
        }
        LogisticsDTO dto = new LogisticsDTO();
        dto.setNumber(number);
        dto.setType(type);
        dto.setList(dto1List);
        String status = jsonObject.get("status").toString();
        if("0".equals(status)){
            String issign = jsonObject1.get("issign").toString();
            String deliverystatus = jsonObject1.get("deliverystatus").toString();
            String courierPhone = jsonObject1.get("courierPhone").toString();
            String expName = jsonObject1.get("expName").toString();
            String expPhone = jsonObject1.get("expPhone").toString();
            String courier = jsonObject1.get("courier").toString();
            String logo = jsonObject1.get("logo").toString();
            String expSite = jsonObject1.get("expSite").toString();
            dto.setIssign(issign);
            dto.setDeliverystatus(deliverystatus);
            dto.setCourierPhone(courierPhone);
            dto.setExpName(expName);
            dto.setExpPhone(expPhone);
            dto.setCourier(courier);
            dto.setLogo(logo);
            dto.setExpSite(expSite);
        }
        return ResponseServer.success("获取成功", dto);
    }


    /**
     * 提现 - 个人余额数据
     * @param request
     * @return
     */
    @GetMapping("/loadingBalanceData")
    public ResponseServer loadingBalanceData(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        Account account = accountDao.queryAccountByUserId(customer.getId());
        List list = new ArrayList();
        BalanceDTO dto = new BalanceDTO();
        dto.setBalanceType("0");
        dto.setBalance(account.getVoucherBalance());
        dto.setCharge(new BigDecimal(0.05));
        list.add(dto);
        BalanceDTO dto1 = new BalanceDTO();
        dto1.setBalanceType("1");
        dto1.setBalance(account.getCollageBalance());
        dto1.setCharge(new BigDecimal(0.05));
        list.add(dto1);
        BalanceDTO dto2 = new BalanceDTO();
        dto2.setBalanceType("2");
        dto2.setBalance(account.getShareBalance());
        dto2.setCharge(new BigDecimal(0.1));
        list.add(dto2);
        return ResponseServer.success("加载成功", list);
    }


    /**
     * 提现 - 获取最近一次提现账户数据
     * @param request
     * @return
     */
    @GetMapping("/getWithdrawalAccountData")
    public ResponseServer getWithdrawalAccountData(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        WechatAlipayDTO dto = new WechatAlipayDTO();
        dto.setUserId(customer.getId());
        List<WechatAlipay> waList = wechatAlipayDao.queryByUserIdAndType(customer.getId(), "0");
        if(waList.size() > 0){
            WechatAlipay wa = waList.get(0);
            dto.setHeadImg(wa.getHeadImg());
            dto.setNickName(wa.getNickName());
            dto.setOpenId(wa.getOpenId());
        }else{
            dto.setHeadImg("");
            dto.setNickName("");
            dto.setOpenId("");
        }
        List<WechatAlipay> waList1 = wechatAlipayDao.queryByUserIdAndType(customer.getId(), "1");
        if(waList1.size() > 0){
            WechatAlipay wa1 = waList1.get(0);
            dto.setAlipayAvatar(wa1.getAlipayAvatar());
            dto.setAlipayNickName(wa1.getAlipayNickName());
            dto.setAlipayUserId(wa1.getAlipayUserId());
        }else{
            dto.setAlipayAvatar("");
            dto.setAlipayNickName("");
            dto.setAlipayUserId("");
        }
        return ResponseServer.success("最近使用账号", dto);
    }


    /**
     * 客户提现
     * @param parameter
     * @param request
     * @return
     */
    @PostMapping("/customerWithdrawCash")
    public ResponseServer customerWithdrawCash(@RequestBody String parameter, HttpServletRequest request){
        System.err.println("客户提现数据********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        JSONObject jsonObject = JSONObject.parseObject(parameter);

        String balanceType = jsonObject.get("balanceType").toString(); // 余额类型（0=余额，1=拼团余额，2=共享余额）

        if("0".equals(balanceType) || "1".equals(balanceType)){
            return ResponseServer.error("暂时无法提现, 请留意公司公告!");
        }

        String arrivalType = jsonObject.get("arrivalType").toString(); // 到账类型（0=微信，1=支付宝）
        String amountStr = jsonObject.get("amount").toString();
        BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(amountStr)); // 提现金额

        String openId = "";
        String nickName = "";
        String headImg = "";
        String alipayNickName = "";
        String alipayAvatar = "";
        String alipayUserId = "";
        if("0".equals(arrivalType)){
            openId = jsonObject.get("openId").toString(); // 个人微信openId
            nickName = jsonObject.get("nickName").toString(); // 微信昵称
            headImg = jsonObject.get("headImg").toString(); // 微信昵称
        }else{
            alipayNickName = jsonObject.get("alipayNickName").toString(); // 支付宝昵称
            alipayAvatar = jsonObject.get("alipayAvatar").toString(); // 支付宝头像
            alipayUserId = jsonObject.get("alipayUserId").toString(); // 支付宝用户ID
        }

        Account account = accountDao.queryAccountByUserId(customer.getId());
        // 提余额
        if("0".equals(balanceType)){
            if(amount.doubleValue() < 10){
                return ResponseServer.error("提现金额不正确, 10元起");
            }
            if(account.getVoucherBalance().doubleValue() < amount.doubleValue()){
                return ResponseServer.error("提现失败, 余额不足");
            }
        }
        // 提拼团
        else if("1".equals(balanceType)){
            if(amount.doubleValue() < 10){
                return ResponseServer.error("提现金额不正确, 10元起");
            }
            if(account.getCollageBalance().doubleValue() < amount.doubleValue()){
                return ResponseServer.error("提现失败, 拼团余额不足");
            }
        }
        // 提共享
        else if("2".equals(balanceType)){
            if(amount.doubleValue() < 100){
                return ResponseServer.error("提现金额不正确, 100元起");
            }
            if(account.getShareBalance().doubleValue() < amount.doubleValue()){
                return ResponseServer.error("提现失败, 共享余额不足");
            }
        }

        // 提现到微信
        if("0".equals(arrivalType)) {
            if("".equals(openId)){
                return ResponseServer.error("微信提现失败, openId为空");
            }
        }
        // 提现到支付宝
        else if("1".equals(arrivalType)){
            if("".equals(alipayUserId)){
                return ResponseServer.error("支付宝提现失败, 支付宝用户ID不能为空");
            }
        }

        BigDecimal chargeAmount = new BigDecimal(0);
        BigDecimal actualAmount = new BigDecimal(0);

        // 提余额5%
        if("0".equals(balanceType)){
            chargeAmount = amount.multiply(BigDecimal.valueOf(0.05));
            actualAmount = amount.subtract(chargeAmount);
        }
        // 提拼团手续费5%
        else if("1".equals(balanceType)){
            chargeAmount = amount.multiply(BigDecimal.valueOf(0.05));
            actualAmount = amount.subtract(chargeAmount);
        }
        // 提共享手续费10%
        else if("2".equals(balanceType)){
            chargeAmount = amount.multiply(BigDecimal.valueOf(0.1));
            actualAmount = amount.subtract(chargeAmount);
        }
        String orderNo = "TX" + Units.getOrderIdByTime().replaceAll("-", "");
        // 提现到微信
        if("0".equals(arrivalType)){
            // 提余额
            if("0".equals(balanceType)){
                account.setVoucherBalance(account.getVoucherBalance().subtract(amount));
            }
            // 提拼团
            else if("1".equals(balanceType)){
                account.setCollageBalance(account.getCollageBalance().subtract(amount));
            }
            // 提共享
            else if("2".equals(balanceType)){
                account.setShareBalance(account.getShareBalance().subtract(amount));
            }
            accountDao.update(account);
            //添加记录
            AccountRecord ar = new AccountRecord();
            ar.setOutUserId(customer.getId());
            ar.setUpdateTime(new Date());
            if("0".equals(balanceType)){
                ar.setType("0");
                ar.setNowAmount(account.getVoucherBalance());
                ar.setInfo("余额提现");
            }
            // 提拼团
            else if("1".equals(balanceType)){
                ar.setType("8");
                ar.setNowAmount(account.getCollageBalance());
                ar.setInfo("拼团余额提现");
            }
            // 提共享
            else if("2".equals(balanceType)){
                ar.setType("10");
                ar.setNowAmount(account.getShareBalance());
                ar.setInfo("共享余额提现");
            }
            ar.setAmount(amount);
            ar.setSign("2");
            ar.setOrderNo(orderNo);
            accountRecordDao.save(ar);
            // 添加提现记录
            Withdrawal w = new Withdrawal();
            w.setActualAmount(actualAmount);
            w.setAmount(amount);
            w.setArrivalType(arrivalType);
            w.setBalanceType(balanceType);
            w.setCreateTime(new Date());
            w.setStatus("0");
            w.setUserId(customer.getId());
            w.setOrderNo(orderNo);
            w.setChargeAmount(chargeAmount);
            w.setConfirmTime(new Date());
            w.setOpenId(openId);
            withdrawalDao.save(w);
            // 保存微信账号
            WechatAlipay wa = new WechatAlipay();
            wa.setUserId(customer.getId());
            wa.setType("0");
            wa.setHeadImg(headImg);
            EmojiConverter emojiConverter = EmojiConverter.getInstance();
            wa.setNickName(emojiConverter.toAlias(nickName));
            wa.setOpenId(openId);
            wechatAlipayDao.save(wa);
        }
        // 提现到支付宝
        else if("1".equals(arrivalType)){
            // 提余额
            if("0".equals(balanceType)){
                account.setVoucherBalance(account.getVoucherBalance().subtract(amount));
            }
            // 提拼团
            else if("1".equals(balanceType)){
                account.setCollageBalance(account.getCollageBalance().subtract(amount));
            }
            // 提共享
            else if("2".equals(balanceType)){
                account.setShareBalance(account.getShareBalance().subtract(amount));
            }
            accountDao.update(account);
            //添加记录
            AccountRecord ar = new AccountRecord();
            ar.setOutUserId(customer.getId());
            ar.setUpdateTime(new Date());
            if("0".equals(balanceType)){
                ar.setType("0");
                ar.setNowAmount(account.getVoucherBalance());
                ar.setInfo("余额提现");
            }
            // 提拼团
            else if("1".equals(balanceType)){
                ar.setType("8");
                ar.setNowAmount(account.getCollageBalance());
                ar.setInfo("拼团余额提现");
            }
            // 提共享
            else if("2".equals(balanceType)){
                ar.setType("10");
                ar.setNowAmount(account.getShareBalance());
                ar.setInfo("共享余额提现");
            }
            ar.setAmount(amount);
            ar.setSign("2");
            ar.setOrderNo(orderNo);
            accountRecordDao.save(ar);
            // 添加提现记录
            Withdrawal w = new Withdrawal();
            w.setActualAmount(actualAmount);
            w.setAmount(amount);
            w.setArrivalType(arrivalType);
            w.setBalanceType(balanceType);
            w.setCreateTime(new Date());
            w.setStatus("0");
            w.setUserId(customer.getId());
            w.setOrderNo(orderNo);
            w.setChargeAmount(chargeAmount);
            w.setConfirmTime(new Date());
            w.setAlipayUserId(alipayUserId);
            withdrawalDao.save(w);
            // 保存支付宝账号
            WechatAlipay wa = new WechatAlipay();
            wa.setUserId(customer.getId());
            wa.setType("1");
            wa.setAlipayAvatar(alipayAvatar);
            wa.setAlipayNickName(alipayNickName);
            wa.setAlipayUserId(alipayUserId);
            wechatAlipayDao.save(wa);
        }
        return ResponseServer.success("提现成功");
    }


    /**
     * 共享余额转账
     * @param parameter
     * @param request
     * @return
     */
    @PostMapping("/shareTransferAccount")
    public ResponseServer shareTransferAccount(@RequestBody String parameter, HttpServletRequest request){
        System.err.println("共享余额转账********** parameter ---------- : " + parameter);
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        JSONObject jsonObject = JSONObject.parseObject(parameter);
        Integer userId = (Integer) jsonObject.get("userId"); // 进账人ID
        if(jsonObject.get("amount") == null || "".equals(jsonObject.get("amount").toString())){
            return ResponseServer.error("转账金额不规范");
        }
        String amountStr = jsonObject.get("amount").toString(); //转入金额
        BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(amountStr));
        if(amount.doubleValue() <= 0){
            return ResponseServer.error("转账金额需大于0");
        }
        Customer outC = customerDao.queryById(userId);
        if(outC == null){
            return ResponseServer.error("转账失败,进账人信息不正确");
        }
        Account out = accountDao.queryAccountByUserId(customer.getId());
        if(out.getShareBalance().doubleValue() < amount.doubleValue()){
            return ResponseServer.error("共享余额不足");
        }
        out.setShareBalance(out.getShareBalance().subtract(amount));
        accountDao.update(out);

        AccountRecord ar = new AccountRecord();
        ar.setOutUserId(customer.getId());
        ar.setUpdateTime(new Date());
        ar.setType("10");
        ar.setAmount(amount);
        ar.setNowAmount(out.getShareBalance());
        ar.setSign("2");
        ar.setInfo("转共享余额给:" + userId);
        accountRecordDao.save(ar);

        Account into = accountDao.queryAccountByUserId(userId);
        into.setShareBalance(into.getShareBalance().add(amount));
        accountDao.update(into);

        AccountRecord ar1 = new AccountRecord();
        ar1.setIntoUserId(userId);
        ar1.setUpdateTime(new Date());
        ar1.setType("10");
        ar1.setAmount(amount);
        ar1.setNowAmount(into.getShareBalance());
        ar1.setSign("1");
        ar1.setInfo("收到:" + customer.getId() + "转入金额:" + amount);
        accountRecordDao.save(ar1);

        return ResponseServer.success("转账成功");
    }


    /**
     * 用户等级数据
     * @param request
     * @return
     */
    @GetMapping("/customerLevelData")
    public ResponseServer customerLevelData(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        CustomerLevelDTO dto = new CustomerLevelDTO();
        dto.setUserId(customer.getId());
        dto.setNickName(customer.getNickName());
        dto.setHeadImg(customer.getHeadImg());
        dto.setAgentCode(customer.getAgentCode());

        dto.setLevelId(customer.getLevelId());
        dto.setIsActive(customer.getIsActive());
        dto.setLevelName(levelDao.queryById(customer.getLevelId()).getName());

        dto.setLevelCopy2Id(customer.getLevelCopy2Id());
        dto.setIsActiveCopy2(customer.getIsActiveCopy2());
        dto.setLevelCopy2Name(levelCopy2Dao.queryById(customer.getLevelCopy2Id()).getName());

        dto.setLevelCopy3Id(customer.getLevelCopy3Id());
        dto.setIsActiveCopy3(customer.getIsActiveCopy3());
        dto.setLevelCopy3Name(levelCopy3Dao.queryById(customer.getLevelCopy3Id()).getName());

        dto.setLevelCopy4Id(customer.getLevelCopy4Id());
        dto.setIsActiveCopy4(customer.getIsActiveCopy4());
        dto.setLevelCopy4Name(levelCopy4Dao.queryById(customer.getLevelCopy4Id()).getName());

        return ResponseServer.success("加载成功", dto);
    }


    /**
     * 奖池擂台排名数据
     * @param request
     * @return
     */
    @GetMapping("/prizePoolArenaRanking")
    public ResponseServer prizePoolArenaRanking(HttpServletRequest request){
        Customer customer = customerDao.queryCustomerByToken(request.getHeader("authorization"));
        if(customer == null){
            return ResponseServer.authorization_expired("授权过期, 请重新授权");
        }
        Map map = new HashMap();
        // 系统当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date toDate = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(toDate);
        String date = sdf.format(now.getTime());
        // 擂台奖金池 4%
        PrizePoolArean arean = prizePoolAreanDao.queryByToday(date); // 今日的
        if(arean == null){
            // 创建今日
            PrizePoolArean newArean = new PrizePoolArean();
            newArean.setUpdateTime(new Date());
            newArean.setSplitMoney(new BigDecimal(0));
            newArean.setPayMoney(BigDecimal.valueOf(0));
            prizePoolAreanDao.save(newArean);
            map.put("prizePollTotalAmount", 0);
        }else{
            map.put("prizePollTotalAmount", arean.getPayMoney());
        }
        List<PrizePoolCustomerDTO> ppcList = customerDao.queryByTodayArean();
        map.put("arenaRankingList", ppcList);
        return ResponseServer.success("加载成功", map);
    }


    // 随机一个六位数
    public static int randoms() {
        int j = (int)((Math.random()*9+1)*100000);
        return j;
    }


}
